Create a gist now

Instantly share code, notes, and snippets.

What would you like to do?
NestedToFlatArray
//
// NestedToFlatTests.m
// NestedToFlatTests
//
// Created by Ishkhan Gevorgyan on 12/3/17.
// Copyright © 2017 Ishkhan Gevorgyan. All rights reserved.
//
#import <XCTest/XCTest.h>
#import "NSMutableArray+FlatArray.h"
@interface NestedToFlatTests : XCTestCase
@end
@implementation NestedToFlatTests
- (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)testExample {
// This is an example of a functional test case.
// Use XCTAssert and related functions to verify your tests produce the correct results.
}
- (void)testPerformanceExample {
// This is an example of a performance test case.
[self measureBlock:^{
// Put the code you want to measure the time of here.
}];
}
- (void)testForNilArray {
NSMutableArray *nestedArray = nil;
NSMutableArray *flatArray = nil;
XCTAssertEqualObjects(flatArray, [nestedArray makeFlatArrayForNumbers], @"Nil case failed to flat");
}
- (void)testForEmptyArray {
NSMutableArray * nestedArray = [NSMutableArray new];
NSMutableArray * flatArray = [NSMutableArray new];
XCTAssertEqualObjects(flatArray, [nestedArray makeFlatArrayForNumbers], @"Empty array failed to flat");
}
- (void)testArray {
NSMutableArray * nestedArray = [NSMutableArray new];
NSArray *array1 = @[@1,
@2,
@[@3, @4],
@[@5],
@[@[@6, @7], @8, @[@9, @[@10], @[]], @[]],
@[],
@11,
@[],
@[@11,@12,@13,@14,@15,@16,@17,@18,@19],
];
nestedArray = [NSMutableArray arrayWithArray:array1];
NSMutableArray * flatArray = [NSMutableArray new];
NSArray *array2 = @[@1, @2, @3, @4, @5, @6, @7, @8, @9, @10, @11,@11,@12,@13,@14,@15,@16,@17,@18,@19];
flatArray = [NSMutableArray arrayWithArray:array2];
XCTAssertEqualObjects(flatArray, [nestedArray makeFlatArrayForNumbers], @"Array failed to flat");
}
@end
//
// NSMutableArray+FlatArray.h
// NestedToFlat
//
// Created by Ishkhan Gevorgyan on 12/3/17.
// Copyright © 2017 Ishkhan Gevorgyan. All rights reserved.
//
#import <Foundation/Foundation.h>
@interface NSMutableArray (FlatArray)
- (NSMutableArray *)makeFlatArrayForNumbers;
@end
//
// NSMutableArray+FlatArray.m
// NestedToFlat
//
// Created by Ishkhan Gevorgyan on 12/3/17.
// Copyright © 2017 Ishkhan Gevorgyan. All rights reserved.
//
#import "NSMutableArray+FlatArray.h"
@implementation NSMutableArray (FlatArray)
-(NSMutableArray *)makeFlatArrayForNumbers // name is in such type to help to use only with Arrays with Numbers
{
NSMutableArray * flatArray = [NSMutableArray new];
//Call to return flat the array
[self makeFlatArrayFrom:self andWriteToArray:flatArray];
return flatArray;
}
//recursive function of flatting Array
-(void)makeFlatArrayFrom:(NSMutableArray *)originalArray andWriteToArray:(NSMutableArray *)resultArray
{
for(id element in originalArray)
{
if([element isKindOfClass:[NSNumber class]])
{
// add element to flat if number
[resultArray addObject:element];
}
else
if ([element isKindOfClass:[NSArray class]] || [element isKindOfClass:[NSMutableArray class]])
{
// recursive call if element is array typed
[self makeFlatArrayFrom:element andWriteToArray:resultArray];
}
else
{
// exeption for using other typed elements
NSLog(@"Object is not of NSNumber or NSArray type: %@", element);
NSException* myException = [NSException
exceptionWithName:@"ObjectNotNumberOrArray"
reason:@"Object is not of NSNumber or NSArray type"
userInfo:nil];
@throw myException;
}
}
}
@end
//
// ViewController.h
// NestedToFlat
//
// Created by Ishkhan Gevorgyan on 12/3/17.
// Copyright © 2017 Ishkhan Gevorgyan. All rights reserved.
//
#import <UIKit/UIKit.h>
@interface ViewController : UIViewController
@end
//
// ViewController.m
// NestedToFlat
//
// Created by Ishkhan Gevorgyan on 12/3/17.
// Copyright © 2017 Ishkhan Gevorgyan. All rights reserved.
//
#import "ViewController.h"
#import "NSMutableArray+FlatArray.h"
@interface ViewController ()
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
[self testFlatten];
}
-(void)testFlatten
{
//For testing we create the nested Array [[1,2,[3]],4]
NSMutableArray * nestedArray = [NSMutableArray new];
NSMutableArray * nest2 = [NSMutableArray new];
[nest2 addObject:@3];
NSMutableArray * nest1 = [NSMutableArray new];
[nest1 addObject:@1];
[nest1 addObject:@2];
[nest1 addObject:nest2];
[nestedArray addObject:nest1];
[nestedArray addObject:@4];
NSMutableArray * flattenArray = [NSMutableArray new];
//Call to flat the array [[1,2,[3]],4] -> [1,2,3,4]
flattenArray = [nestedArray makeFlatArrayForNumbers]; // usage
NSLog(@"nested Array is%@",nestedArray);
NSLog(@"flat Array is%@",flattenArray);
}
@end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment