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

ingemar's solution

to Strain in the Objective-C Track

Published at Jul 22 2019 · 0 comments
Instructions
Test suite
Solution

Implement the keep and discard operation on collections. Given a collection and a predicate on the collection's elements, keep returns a new collection containing those elements where the predicate is true, while discard returns a new collection containing those elements where the predicate is false.

For example, given the collection of numbers:

1, 2, 3, 4, 5 And the predicate:

is the number even? Then your keep operation should produce:

2, 4 While your discard operation should produce:

1, 3, 5 Note that the union of keep and discard is all the elements.

The functions may be called keep and discard, or they may need different names in order to not clash with existing functions or concepts in your language.

Restrictions -

The filter/reject/whatchamacallit functionality provided by the standard library should not be used! This should be solved using other basic tools instead.

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/strain/Strain.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.

StrainTest.m

#import <XCTest/XCTest.h>

#if __has_include("StrainExample.h")
# import "StrainExample.h"
# else
# import "Strain.h"
#endif

@interface StrainTest : XCTestCase

@end

@implementation StrainTest

- (void)setUp {
    [super setUp];
    // Put setup code here. This method is called before the invocation of each test method in the class.
}

- (void)tearDown {
    // Put teardown code here. This method is called after the invocation of each test method in the class.
    [super tearDown];
}

- (void)testEmptyKeep {
    
    NSArray *inputArray = @[];
    
    NSArray *expectedArray = inputArray;
    
    XCTAssertEqualObjects(expectedArray, [inputArray keep:true where:^BOOL(id  _Nonnull arrayElement) {
        
        NSNumber *number = (NSNumber*)arrayElement;
        int integerNumber = number.intValue;
        return integerNumber < 10;
        
    }]);
}

- (void)testKeepEverything {
    
    NSArray *inputArray = @[@1,@2,@3];
    
    NSArray *expectedArray = inputArray;
    
    XCTAssertEqualObjects(expectedArray, [inputArray keep:true where:^BOOL(id  _Nonnull arrayElement) {
        
        NSNumber *number = (NSNumber*)arrayElement;
        int integerNumber = number.intValue;
        return integerNumber < 10;
        
    }]);
}

-(void)testKeepFirstAndLastElements {
    
    NSArray *inputArray = @[@1,@2,@3];
    
    NSArray *expectedArray = @[@1,@3];
    
    XCTAssertEqualObjects(expectedArray, [inputArray keep:true where:^BOOL(id  _Nonnull arrayElement) {
        
        NSNumber *number = (NSNumber*)arrayElement;
        int integerNumber = number.intValue;
        return integerNumber % 2 != 0;
        
    }]);
}

-(void)testKeepNeitherFirstNorLastElement {
    
    NSArray *inputArray = @[@1,@2,@3,@4,@5];
    
    NSArray *expectedArray = @[@2,@4];
    
    XCTAssertEqualObjects(expectedArray, [inputArray keep:true where:^BOOL(id  _Nonnull arrayElement) {
        
        NSNumber *number = (NSNumber*)arrayElement;
        int integerNumber = number.intValue;
        return integerNumber % 2 == 0;
        
    }]);
}

-(void)testKeepStrings {
    
    NSArray *inputArray = @[@"apple",@"zebra",@"banana",@"Zombies",@"cherimoya",@"zealot"];
    
    NSArray *expectedArray = @[@"zebra",@"Zombies",@"zealot"];
    
    XCTAssertEqualObjects(expectedArray, [inputArray keep:true where:^BOOL(id  _Nonnull arrayElement) {
        
        NSString *string = (NSString*)arrayElement;
        
        return ([string rangeOfString:@"z"].location != NSNotFound || [string rangeOfString:@"Z"].location != NSNotFound);
        
    }]);
}

-(void)testKeepArrays {
    
    NSArray *inputArray = @[@[@1,@2,@3],@[@5,@5,@5],@[@5,@1,@2],@[@2,@1,@2],@[@1,@5,@2],@[@2,@2,@1],@[@1,@2,@5]];
    
    NSArray *expectedArray = @[@[@5,@5,@5],@[@5,@1,@2],@[@1,@5,@2],@[@1,@2,@5]];
    
    XCTAssertEqualObjects(expectedArray, [inputArray keep:true where:^BOOL(id  _Nonnull arrayElement) {
        
        NSArray *nestedArray = (NSArray*)arrayElement;
        return [nestedArray containsObject:@5];
        
    }]);
   
}

-(void)testDiscardEmptyArray {
    
    NSArray *inputArray = @[];
    
    NSArray *expectedArray = inputArray;
    
    XCTAssertEqualObjects(expectedArray, [inputArray discard:^BOOL(id  _Nonnull arrayElement) {
        
        NSNumber *number = (NSNumber*)arrayElement;
        int integerNumber = number.intValue;
        return integerNumber < 10;
        
    }]);
    
}

-(void)testDiscardNothing {
    
    NSArray *inputArray = @[@1,@2,@3];
    
    NSArray *expectedArray = inputArray;
    
    XCTAssertEqualObjects(expectedArray, [inputArray discard:^BOOL(id  _Nonnull arrayElement) {
        
        NSNumber *number = (NSNumber*)arrayElement;
        int integerNumber = number.intValue;
        return integerNumber > 10;
        
    }]);
}

-(void)testDiscardFirstAndLastElements {
    
    NSArray *inputArray = @[@1,@2,@3];
    
    NSArray *expectedArray = @[@2];
    
    XCTAssertEqualObjects(expectedArray, [inputArray discard:^BOOL(id  _Nonnull arrayElement) {
        
        NSNumber *number = (NSNumber*)arrayElement;
        int integerNumber = number.intValue;
        return integerNumber % 2 != 0;
        
    }]);
}

-(void)testDiscardNeitherFirstNorLastElement {
    
    NSArray *inputArray = @[@1,@2,@3,@4,@5];
    
    NSArray *expectedArray = @[@1,@3,@5];
    
    XCTAssertEqualObjects(expectedArray, [inputArray discard:^BOOL(id  _Nonnull arrayElement) {
        
        NSNumber *number = (NSNumber*)arrayElement;
        int integerNumber = number.intValue;
        return integerNumber % 2 == 0;
        
    }]);
}

-(void)testDiscardStrings {
    
    NSArray *inputArray = @[@"apple",@"zebra",@"banana",@"Zombies",@"cherimoya",@"zealot"];
    
    NSArray *expectedArray = @[@"apple",@"banana",@"cherimoya"];
    
    XCTAssertEqualObjects(expectedArray, [inputArray discard:^BOOL(id  _Nonnull arrayElement) {
        
        NSString *string = (NSString*)arrayElement;
        
        return ([string rangeOfString:@"z"].location != NSNotFound || [string rangeOfString:@"Z"].location != NSNotFound);
      
    }]);
}

-(void)testDiscardArrays {
    
    NSArray *inputArray = @[@[@1,@2,@3],@[@5,@5,@5],@[@5,@1,@2],@[@2,@1,@2],@[@1,@5,@2],@[@2,@2,@1],@[@1,@2,@5]];
    
    NSArray *expectedArray = @[@[@1,@2,@3],@[@2,@1,@2],@[@2,@2,@1]];
    
    XCTAssertEqualObjects(expectedArray, [inputArray discard:^BOOL(id  _Nonnull arrayElement) {
        
        NSArray *nestedArray = (NSArray*)arrayElement;
        return [nestedArray containsObject:@5];
        
    }]);
}

-(void)testKeepOnMutableArrays {
    
    NSMutableArray *inputArray = [NSMutableArray arrayWithObjects:@[@1,@2,@3],@[@5,@5,@5],@[@5,@1,@2],@[@2,@1,@2],@[@1,@5,@2],@[@2,@2,@1],@[@1,@2,@5], nil];
    
    NSArray *expectedArray = @[@[@5,@5,@5],@[@5,@1,@2],@[@1,@5,@2],@[@1,@2,@5]];
    
    XCTAssertEqualObjects(expectedArray, [inputArray keep:true where:^BOOL(id  _Nonnull arrayElement) {
        
        NSArray *nestedArray = (NSArray*)arrayElement;
        return [nestedArray containsObject:@5];
        
    }]);
}

-(void)testDiscardOnMutableArrays {
    
    NSMutableArray *inputArray = [NSMutableArray arrayWithObjects:@[@1,@2,@3],@[@5,@5,@5],@[@5,@1,@2],@[@2,@1,@2],@[@1,@5,@2],@[@2,@2,@1],@[@1,@2,@5], nil];
    
    NSArray *expectedArray = @[@[@1,@2,@3],@[@2,@1,@2],@[@2,@2,@1]];
    
    XCTAssertEqualObjects(expectedArray, [inputArray discard:^BOOL(id  _Nonnull arrayElement) {
        
        NSArray *nestedArray = (NSArray*)arrayElement;
        return [nestedArray containsObject:@5];
        
    }]);
}

@end

strain-tests/Strain.h

//
//  Strain.h
//  strain-tests
//
//  Created by Ingemar Pertl on 22.07.19.
//  Copyright © 2019 Ingemar Pertl. All rights reserved.
//

#import <Foundation/Foundation.h>

NS_ASSUME_NONNULL_BEGIN

@interface NSArray (Strain)

- (NSArray *)keep:(BOOL)shouldKeep where:(BOOL (^)(id input))condition;

- (NSArray *)discard:(BOOL (^)(id input))condition;

@end

NS_ASSUME_NONNULL_END

strain-tests/Strain.m

//
//  Strain.m
//  strain-tests
//
//  Created by Ingemar Pertl on 22.07.19.
//  Copyright © 2019 Ingemar Pertl. All rights reserved.
//

#import "Strain.h"

@implementation NSArray (Strain)

- (NSArray *)keep:(BOOL)shouldKeep where:(BOOL (^)(id input))condition {
    NSMutableArray *newArray = [NSMutableArray new];
    [self enumerateObjectsUsingBlock:^(id  _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
        if(condition(obj) && shouldKeep)
            [newArray addObject:obj];
    }];
    return newArray;
}

- (NSArray *)discard:(BOOL (^)(id input))condition {
    NSMutableArray *newArray = [NSMutableArray arrayWithArray:self];
    [self enumerateObjectsUsingBlock:^(id  _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
        if(condition(obj))
            [newArray removeObject:obj];
    }];
    return newArray;
}

@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?