ðŸŽ‰ Exercism Research is now launched. Help Exercism, help science and have some fun at research.exercism.io ðŸŽ‰

# AllenLaiShiuanFarn's solution

## to Sublist in the Objective-C Track

Published at Nov 23 2018 · 0 comments
Instructions
Test suite
Solution

Given two lists determine if the first list is contained within the second list, if the second list is contained within the first list, if both lists are contained within each other or if none of these are true.

Specifically, a list A is a sublist of list B if by dropping 0 or more elements from the front of B and 0 or more elements from the back of B you get a list that's completely equal to A.

Examples:

• A = [1, 2, 3], B = [1, 2, 3, 4, 5], A is a sublist of B
• A = [3, 4, 5], B = [1, 2, 3, 4, 5], A is a sublist of B
• A = [3, 4], B = [1, 2, 3, 4, 5], A is a sublist of B
• A = [1, 2, 3], B = [1, 2, 3], A is equal to B
• A = [1, 2, 3, 4, 5], B = [2, 3, 4], A is a superlist of B
• A = [1, 2, 4], B = [1, 2, 3, 4, 5], A is not a superlist of, sublist of or equal to B

## 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/sublist/Sublist.m
``````

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

## Submitting Incomplete Solutions

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

### SublistTest.m

``````#import <XCTest/XCTest.h>

#if __has_include("SublistExample.h")
# import "SublistExample.h"
# else
# import "Sublist.h"
#endif

@interface SublistTest : XCTestCase

@end

@implementation SublistTest

- (void)testEmptyLists {
XCTAssertEqual(SublistKindEqual, [SublistExample classifierForFirstList:@[] andSecondList:@[]]);
}

- (void)testEmptyListWithinNonEmptyList {
NSArray *secondList = @[@1, @2, @3];
XCTAssertEqual(SublistKindSublist, [SublistExample classifierForFirstList:@[] andSecondList:secondList]);
}

- (void)testNonEmptyListContainsEmptyList {
NSArray *firstList = @[@1, @2, @3];
XCTAssertEqual(SublistKindSuperlist, [SublistExample classifierForFirstList:firstList andSecondList:@[]]);
}

- (void)testListEqualsItself {
NSArray *firstList = @[@1, @2, @3];
NSArray *secondList = @[@1, @2, @3];
XCTAssertEqual(SublistKindEqual, [SublistExample classifierForFirstList:firstList andSecondList:secondList]);
}

- (void)testDifferentLists {
NSArray *firstList = @[@1, @2, @3];
NSArray *secondList = @[@2, @3, @4];
XCTAssertEqual(SublistKindUnequal, [SublistExample classifierForFirstList:firstList andSecondList:secondList]);
}

- (void)testFalseStart {
NSArray *firstList = @[@1, @2, @5];
NSArray *secondList = @[@0, @1, @2, @3, @1, @2, @5, @6];
XCTAssertEqual(SublistKindSublist, [SublistExample classifierForFirstList:firstList andSecondList:secondList]);
}

- (void)testConsecutive {
NSArray *firstList = @[@1, @1, @2];
NSArray *secondList = @[@0, @1, @1, @1, @2, @1, @2];
XCTAssertEqual(SublistKindSublist, [SublistExample classifierForFirstList:firstList andSecondList:secondList]);
}

- (void)testSublistAtStart {
NSArray *firstList = @[@0, @1, @2];
NSArray *secondList = @[@0, @1, @2, @3, @4, @5];
XCTAssertEqual(SublistKindSublist, [SublistExample classifierForFirstList:firstList andSecondList:secondList]);
}

- (void)testSublistInMiddle{
NSArray *firstList = @[@2, @3, @4];
NSArray *secondList = @[@0, @1, @2, @3, @4, @5];
XCTAssertEqual(SublistKindSublist, [SublistExample classifierForFirstList:firstList andSecondList:secondList]);
}

- (void)testSublistAtEnd {
NSArray *firstList = @[@3, @4, @5];
NSArray *secondList = @[@0, @1, @2, @3, @4, @5];
XCTAssertEqual(SublistKindSublist, [SublistExample classifierForFirstList:firstList andSecondList:secondList]);
}

- (void)testAtStartOfSuperlist {
NSArray *firstList = @[@0, @1, @2, @3, @4, @5];
NSArray *secondList = @[@0, @1, @2];
XCTAssertEqual(SublistKindSuperlist, [SublistExample classifierForFirstList:firstList andSecondList:secondList]);
}

- (void)testAtMiddleOfSuperlist {
NSArray *firstList = @[@0, @1, @2, @3, @4, @5];
NSArray *secondList = @[@2, @3];
XCTAssertEqual(SublistKindSuperlist, [SublistExample classifierForFirstList:firstList andSecondList:secondList]);
}

- (void)testAtEndOfSuperlist {
NSArray *firstList = @[@0, @1, @2, @3, @4, @5];
NSArray *secondList = @[@3, @4, @5];
XCTAssertEqual(SublistKindSuperlist, [SublistExample classifierForFirstList:firstList andSecondList:secondList]);
}

- (void)testFirstListMissingElementFromSecondList {
NSArray *firstList = @[@1, @3];
NSArray *secondList = @[@1, @2, @3];
XCTAssertEqual(SublistKindUnequal, [SublistExample classifierForFirstList:firstList andSecondList:secondList]);
}

- (void)testSecondListMissingOneElementFromFirstList {
NSArray *firstList = @[@1, @2, @3];
NSArray *secondList = @[@1, @3];
XCTAssertEqual(SublistKindUnequal, [SublistExample classifierForFirstList:firstList andSecondList:secondList]);
}

- (void)testOrderMattersToAList {
NSArray *firstList = @[@1, @2, @3];
NSArray *secondList = @[@3, @2, @1];
XCTAssertEqual(SublistKindUnequal, [SublistExample classifierForFirstList:firstList andSecondList:secondList]);
}

- (void)testSameDigitsButDifferentNumbers {
NSArray *firstList = @[@1, @0, @1];
NSArray *secondList = @[@10, @1];
XCTAssertEqual(SublistKindUnequal, [SublistExample classifierForFirstList:firstList andSecondList:secondList]);
}

@end``````
``````//
//  SublistExample.m
//  Sublist
//
//  Created by AllenLai on 2018/11/15.
//

#import "Sublist.h"

@implementation SublistExample

+ (SublistKind)classifierForFirstList:(NSArray<NSNumber *> *)firstList andSecondList:(NSArray<NSNumber *> *)secondList {
NSString *firstString = @",";
NSString *secondString = @",";

if ([firstList count] > 0) {
firstString = [firstList componentsJoinedByString:@","];
}

if ([secondList count] > 0) {
secondString = [secondList componentsJoinedByString:@","];
}

if ([firstString isEqualToString:secondString]) {
return SublistKindEqual;
} else if ([firstString containsString:secondString]) {
return SublistKindSuperlist;
} else if ([secondString containsString:firstString]) {
return SublistKindSublist;
} else {
return SublistKindUnequal;
}
}

@end``````