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

jaishreedhage's solution

to Sublist in the Objective-C Track

Published at Jul 13 2018 · 1 comment
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

Sublist/SublistTest/SublistExample.h

//
//  SublistExample.h
//  Sublist
//
//  Created by Jaishree Dhage on 09/12/17.
//  Copyright © 2017 Jaishree Dhage. All rights reserved.
//

#import <Foundation/Foundation.h>

@interface SublistExample : NSObject

+(BOOL) classifierForFirstList:(NSArray *)firstList andSecondList:(NSArray *)secondList;

@end

Sublist/SublistTest/SublistExample.m

//
//  SublistExample.m
//  Sublist
//
//  Created by Jaishree Dhage on 09/12/17.
//  Copyright © 2017 Jaishree Dhage. All rights reserved.
//

#import "SublistExample.h"

@implementation SublistExample

+(BOOL)classifierForFirstList:(NSArray *)firstList andSecondList:(NSArray *)secondList{
    
    //NOT SURE HOW TO DO
    
    NSUInteger firstLength = [firstList count];
    NSUInteger secondLength = [secondList count];
    for(int i=0;i<[secondList count];i++){
        NSArray *copy = [[NSArray alloc]init];
        [secondList getObjects:copy range:[NSMakeRange(i, secondLength)]];
        if([firstList isEqualToArray:copy])
            return 1;
    }
}

@end

Community comments

Find this solution interesting? Ask the author a question to learn more.
Avatar of jaishreedhage
jaishreedhage
Solution Author
commented over 3 years ago

Not sure how to do.

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?