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

# AllenLaiShiuanFarn's solution

## to Flatten Array in the Objective-C Track

Published at Nov 13 2018 · 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
//
//  FlattenArray.m
//  FlattenArray
//
//  Created by AllenLai on 2018/11/12.
//

#import "FlattenArray.h"

@implementation FlattenArrayExample

+ (NSArray *)flattenArray:(NSArray *)inputArray {
NSMutableArray *outputArray = [NSMutableArray array];
for (id subItem in inputArray) {
if ([subItem isKindOfClass:[NSArray class]]) {
} else if ([subItem isKindOfClass:[NSNull class]]) {
continue;
} else {
}
}
return outputArray;
}

@end