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

ingemar's solution

to Flatten Array in the Objective-C Track

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

Take a nested list and return a single flattened list with all values except nil/null.

The challenge is to write a function that accepts an arbitrarily-deep nested list-like structure and returns a flattened structure without any nil/null values.

For Example

input: [1,[2,3,null,4],[null],5]

output: [1,2,3,4,5]

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/flatten-array/FlattenArray.m

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

Source

Interview Question https://reference.wolfram.com/language/ref/Flatten.html

Submitting Incomplete Solutions

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

FlattenArrayTest.m

#import <XCTest/XCTest.h>

#if __has_include("FlattenArrayExample.h")
# import "FlattenArrayExample.h"
# else
# import "FlattenArray.h"
#endif

@interface FlattenArrayTest : XCTestCase

@end

static NSNull *nullValue;

@implementation FlattenArrayTest

- (void)setUp {
    nullValue = [NSNull null];
}

- (void)testFlattenArrayWithJustIntegers {
    NSArray *inputArray = @[@1, @[@2, @3, @4, @5, @6, @7], @8];
    NSArray *expected = @[@1, @2, @3, @4, @5, @6, @7, @8];
    XCTAssertEqualObjects(expected, [FlattenArrayExample flattenArray:inputArray]);
}

- (void)testFiveLevelNesting {
    NSArray *inputArray = @[@0, @2, @[@[@2, @3], @8, @100, @4, @[@[@50]]], @-2];
    NSArray *expected = @[@0, @2, @2, @3, @8, @100, @4, @50, @-2];
    XCTAssertEqualObjects(expected, [FlattenArrayExample flattenArray:inputArray]);
}

- (void)testSixLevelNesting {
    NSArray *inputArray = @[@1, @[@2, @[@[@3]], @[@4, @[@[@5]]], @6, @7], @8];
    NSArray *expected = @[@1, @2, @3, @4, @5, @6, @7, @8];
    XCTAssertEqualObjects(expected, [FlattenArrayExample flattenArray:inputArray]);
}

- (void)testSixLevelNestingWithNullValues {
    NSArray *inputArray = @[@0, @2, @[@[@2, @3], @8, @[@[@100]], nullValue, @[@[nullValue]]], @-2];
    NSArray *expected = @[@0, @2, @2, @3, @8, @100, @-2];
    XCTAssertEqualObjects(expected, [FlattenArrayExample flattenArray:inputArray]);
}

- (void)testAllValuesInNestedListAreNull {
    NSArray *inputArray = @[nullValue, @[@[@[@[nullValue]]]], nullValue, nullValue, @[@[nullValue, nullValue], nullValue], nullValue];
    NSArray *expected = @[];
    XCTAssertEqualObjects(expected, [FlattenArrayExample flattenArray:inputArray]);
}

- (void)testFiveLevelDeepNestedListWithDoubleValuesAndNullValues {
    NSArray *inputArray = @[@0.085, @2.8844557, @[@[@2.00005, @3.13], @8.4, @100, @4.8, @[@[@[nullValue]]]], @-2.5];
    NSArray *expected = @[@0.085, @2.8844557, @2.00005, @3.13, @8.4, @100, @4.8, @-2.5];
    XCTAssertEqualObjects(expected, [FlattenArrayExample flattenArray:inputArray]);
}

- (void)testSixLevelDeepNestedListWithStringsAndNullValues {
    NSArray *inputArray = @[@"Zero", @"two", @[@[@[@"Two", @"three"]], @"eight", @[@[@"One Hundred"]], nullValue, @[@[nullValue]]], @"minus two"];
    NSArray *expected = @[@"Zero", @"two", @"Two", @"three", @"eight", @"One Hundred", @"minus two"];
    XCTAssertEqualObjects(expected, [FlattenArrayExample flattenArray:inputArray]);
}
@end

flatten-array-tests/FlattenArray.h

//
//  FlattenArray.h
//  flatten-array-tests
//
//  Created by Ingemar Pertl on 16.07.19.
//  Copyright © 2019 Ingemar Pertl. All rights reserved.
//

#import <Foundation/Foundation.h>

NS_ASSUME_NONNULL_BEGIN

@interface FlattenArrayExample : NSObject

+(NSArray*)flattenArray:(NSArray*)input;

@end

NS_ASSUME_NONNULL_END

flatten-array-tests/FlattenArray.m

//
//  FlattenArray.m
//  flatten-array-tests
//
//  Created by Ingemar Pertl on 16.07.19.
//  Copyright © 2019 Ingemar Pertl. All rights reserved.
//

#import "FlattenArray.h"

@implementation FlattenArrayExample

+(NSArray*)flattenArray:(NSArray*)input {
    NSMutableArray *flattened = [NSMutableArray new];
    
    for (int i = 0; i < [input count]; i++) {
        if([input[i] isKindOfClass:[NSNull class]])
            break;
        
        if([input[i] isKindOfClass:[NSArray class]])
            [flattened addObjectsFromArray:[FlattenArrayExample flattenArray:input[i]]];
        else
            [flattened addObject:input[i]];
    }
    
    return flattened;
}

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