🎉 Exercism Research is now launched. Help Exercism, help science and have some fun at research.exercism.io 🎉
Avatar of KiligWY

KiligWY's solution

to Series in the Objective-C Track

Published at Jul 13 2018 · 0 comments
Instructions
Test suite
Solution

Given a string of digits, output all the contiguous substrings of length n in that string.

For example, the string "49142" has the following 3-digit series:

  • 491
  • 914
  • 142

And the following 4-digit series:

  • 4914
  • 9142

And if you ask for a 6-digit series from a 5-digit string, you deserve whatever you get.

Note that these series are only required to occupy adjacent positions in the input; the digits need not be numerically consecutive.

Setup

There are two different methods of getting set up to run the tests with Objective-C:

  • Create an Xcode project with a test target which will run the tests.
  • Use the ruby gem objc as a test runner utility.

Both are described in more detail here: http://exercism.io/languages/objective-c

Submitting Exercises

When submitting an exercise, make sure your solution file is in the same directory as the test code.

The submit command will look something like:

exercism submit <path-to-exercism-workspace>/objective-c/sieve/Sieve.m

You can find the Exercism workspace by running exercism debug and looking for the line beginning with Workspace.

Source

A subset of the Problem 8 at Project Euler http://projecteuler.net/problem=8

Submitting Incomplete Solutions

It's possible to submit an incomplete solution so you can see how others have completed the exercise.

SeriesTest.m

#import <XCTest/XCTest.h>

#if __has_include("SeriesExample.h")
#import "SeriesExample.h"
#else
#import "Series.h"
#endif

@interface SeriesTest : XCTestCase

@end

@implementation SeriesTest

- (void)testSimpleSlicesOfOne {
    Series *series = [[Series alloc] initWithNumberString:@"01234"];
    NSArray *expected = @[@[@0], @[@1], @[@2], @[@3], @[@4]];
    XCTAssertEqualObjects(expected, [series slicesWithSize:1]);
}

- (void)testSimpleSlicesOfOneAgain {
    Series *series = [[Series alloc] initWithNumberString:@"92834"];
    NSArray *expected = @[@[@9], @[@2], @[@8], @[@3], @[@4]];
    XCTAssertEqualObjects(expected, [series slicesWithSize:1]);
}

- (void)testSimpleSlicesOfTwo {
    Series *series = [[Series alloc] initWithNumberString:@"01234"];
    NSArray *expected = @[@[@0, @1], @[@1, @2], @[@2, @3], @[@3, @4]];
    XCTAssertEqualObjects(expected, [series slicesWithSize:2]);
}

- (void)testOtherSlicesOfTwo {
    Series *series = [[Series alloc] initWithNumberString:@"98273463"];
    NSArray *expected = @[@[@9, @8], @[@8, @2], @[@2, @7], @[@7, @3], @[@3, @4], @[@4, @6], @[@6, @3]];
    XCTAssertEqualObjects(expected, [series slicesWithSize:2]);
}

- (void)testSimpleSlicesOfTwoAgain {
    Series *series = [[Series alloc] initWithNumberString:@"37103"];
    NSArray *expected = @[@[@3, @7], @[@7, @1], @[@1, @0], @[@0, @3]];
    XCTAssertEqualObjects(expected, [series slicesWithSize:2]);
}

- (void)testSimpleSlicesOfThree {
    Series *series = [[Series alloc] initWithNumberString:@"01234"];
    NSArray *expected = @[@[@0, @1, @2], @[@1, @2, @3], @[@2, @3, @4]];
    XCTAssertEqualObjects(expected, [series slicesWithSize:3]);
}

- (void)testSimpleSlicesOfThreeAgain {
    Series *series = [[Series alloc] initWithNumberString:@"31001"];
    NSArray *expected = @[@[@3, @1, @0], @[@1, @0, @0], @[@0, @0, @1]];
    XCTAssertEqualObjects(expected, [series slicesWithSize:3]);
}

- (void)testOtherSlicesOfThree {
    Series *series = [[Series alloc] initWithNumberString:@"982347"];
    NSArray *expected = @[@[@9, @8, @2], @[@8, @2, @3], @[@2, @3, @4], @[@3, @4, @7]];
    XCTAssertEqualObjects(expected, [series slicesWithSize:3]);
}

- (void)testSimpleSlicesOfFour {
    Series *series = [[Series alloc] initWithNumberString:@"01234"];
    NSArray *expected = @[@[@0, @1, @2, @3], @[@1, @2, @3, @4]];
    XCTAssertEqualObjects(expected, [series slicesWithSize:4]);
}

- (void)testSimpleSlicesOfFourAgain {
    Series *series = [[Series alloc] initWithNumberString:@"91274"];
    NSArray *expected = @[@[@9, @1, @2, @7], @[@1, @2, @7, @4]];
    XCTAssertEqualObjects(expected, [series slicesWithSize:4]);
}

- (void)testSimpleSlicesOfFive {
    Series *series = [[Series alloc] initWithNumberString:@"01234"];
    NSArray *expected = @[@[@0, @1, @2, @3, @4]];
    XCTAssertEqualObjects(expected, [series slicesWithSize:5]);
}

- (void)testSimpleSlicesOfFiveAgain {
    Series *series = [[Series alloc] initWithNumberString:@"81228"];
    NSArray *expected = @[@[@8, @1, @2, @2, @8]];
    XCTAssertEqualObjects(expected, [series slicesWithSize:5]);
}

- (void)testSimpleSliceThatBlowsUp {
    Series *series = [[Series alloc] initWithNumberString:@"01234"];
    XCTAssertNil([series slicesWithSize:6]);
}

- (void)testMoreComplicatedSliceThatBlowsUp {
    Series *series = [[Series alloc] initWithNumberString:@"01032987583"];
    XCTAssertNil([series slicesWithSize:12]);
}

@end

Series.h

#import <Foundation/Foundation.h>

@interface Series : NSObject

- (instancetype)initWithNumberString:(NSString *)input;
- (NSArray *)slicesWithSize:(NSInteger)size;

@end

Series.m

#import "Series.h"

@implementation Series {
    NSString *_kInput;
}

- (instancetype)initWithNumberString:(NSString *)input {
    if (self = [super init]) {
        _kInput = input;
    }
    return self;
}

- (NSArray *)slicesWithSize:(NSInteger)size {
    if (size > _kInput.length) {
        return nil;
    }
    
    NSMutableArray *inputArray = [NSMutableArray array];
    for (NSInteger i = 0; i < _kInput.length; ++i) {
        [inputArray addObject:@([[_kInput substringWithRange:NSMakeRange(i, 1)] integerValue])];
    }
    
    NSMutableArray *result = [NSMutableArray array];
    for (NSInteger i = 0; i <= _kInput.length - size; ++i) {
        [result addObject:[inputArray subarrayWithRange:NSMakeRange(i, size)]];
    }
    return result;
}

@end

Community comments

Find this solution interesting? Ask the author a question to learn more.

What can you learn from this solution?

A huge amount can be learned from reading other people’s code. This is why we wanted to give exercism users the option of making their solutions public.

Here are some questions to help you reflect on this solution and learn the most from it.

  • What compromises have been made?
  • Are there new concepts here that you could read more about to improve your understanding?