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

# KiligWY's solution

## to Pascal's Triangle in the Objective-C Track

Published at Jul 13 2018 · 0 comments
Instructions
Test suite
Solution

Compute Pascal's triangle up to a given number of rows.

In Pascal's Triangle each number is computed by adding the numbers to the right and left of the current position in the previous row.

``````    1
1 1
1 2 1
1 3 3 1
1 4 6 4 1
# ... etc
``````

## 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/pascals-triangle/PascalsTriangle.m
``````

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

## Source

Pascal's Triangle at Wolfram Math World http://mathworld.wolfram.com/PascalsTriangle.html

## Submitting Incomplete Solutions

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

### PascalsTriangleTest.m

``````#import <XCTest/XCTest.h>

#if __has_include("PascalsTriangleExample.h")
# import "PascalsTriangleExample.h"
# else
# import "PascalsTriangle.h"
#endif

@interface PascalsTriangleTest : XCTestCase

@end

@implementation PascalsTriangleTest

- (void)testOneRow {
PascalsTriangle *triangle = [[PascalsTriangle alloc] initWithNumberOfRows:1];
XCTAssertEqualObjects(@[@[@1]], triangle.rows);
}

- (void)testTwoRows {
PascalsTriangle *triangle = [[PascalsTriangle alloc] initWithNumberOfRows:2];
NSArray *expected = @[@[@1], @[@1, @1]];
XCTAssertEqualObjects(expected, triangle.rows);
}

- (void)testThreeRows {
PascalsTriangle *triangle = [[PascalsTriangle alloc] initWithNumberOfRows:3];
NSArray *expected = @[@[@1], @[@1, @1], @[@1, @2, @1]];
XCTAssertEqualObjects(expected, triangle.rows);
}

- (void)testFourthRow {
PascalsTriangle *triangle = [[PascalsTriangle alloc] initWithNumberOfRows:4];
NSArray *expected = @[@1, @3, @3, @1];
XCTAssertEqualObjects(expected, [triangle.rows lastObject]);
}

- (void)testFifthRow {
PascalsTriangle *triangle = [[PascalsTriangle alloc] initWithNumberOfRows:5];
NSArray *expected = @[@1, @4, @6, @4, @1];
XCTAssertEqualObjects(expected, [triangle.rows lastObject]);
}

- (void)testTwentiethRow {
PascalsTriangle *triangle = [[PascalsTriangle alloc] initWithNumberOfRows:20];
NSArray *expected = @[
@1, @19, @171, @969, @3876, @11628, @27132, @50388, @75582, @92378, @92378,
@75582, @50388, @27132, @11628, @3876, @969, @171, @19, @1
];
XCTAssertEqualObjects(expected, [triangle.rows lastObject]);
}

@end``````

### PascalsTriangle.h

``````#import <Foundation/Foundation.h>

@interface PascalsTriangle : NSObject

@property (nonatomic, strong) NSArray *rows;

- (instancetype)initWithNumberOfRows:(NSInteger)rows;

@end``````

### PascalsTriangle.m

``````#import "PascalsTriangle.h"

@implementation PascalsTriangle

- (instancetype)initWithNumberOfRows:(NSInteger)rows {
if (self = [super init]) {
NSMutableArray *rowsArray = [NSMutableArray array];
for (NSInteger i = 0; i < rows; ++i) {
if (i == 0) {
} else if (i == 1) {
} else {
NSMutableArray *array = [NSMutableArray arrayWithObject:@1];
NSArray *lastRowArray = [rowsArray lastObject];
for (NSInteger j = 1; j < [lastRowArray count]; ++j) {
[array addObject:@([lastRowArray[j - 1] integerValue] + [lastRowArray[j] integerValue])];
}
}
}
self.rows = rowsArray;
}
return self;
}

@end``````