🎉 Exercism Research is now launched. Help Exercism, help science and have some fun at research.exercism.io 🎉 # jaishreedhage'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/PascalsTriangleTest/PascalsTriangle.h

``````//
//  PascalsTriangle.h
//  PascalsTriangle
//
//  Created by Jaishree Dhage on 11/12/17.
//  Copyright © 2017 Jaishree Dhage. All rights reserved.
//

#import <Foundation/Foundation.h>

@interface PascalsTriangle : NSObject

@property (nonatomic,strong,readonly) NSArray * rows;
@property (nonatomic,readonly) NSInteger numberOfRows;

-(instancetype)initWithNumberOfRows:(NSUInteger) rows;

@end``````

### PascalsTriangle/PascalsTriangleTest/PascalsTriangle.m

``````//
//  PascalsTriangle.m
//  PascalsTriangle
//
//  Created by Jaishree Dhage on 11/12/17.
//  Copyright © 2017 Jaishree Dhage. All rights reserved.
//

#import "PascalsTriangle.h"

@interface PascalsTriangle ()

@property (nonatomic,strong,readwrite) NSArray * rows;
@property (nonatomic,readwrite) NSInteger numberOfRows;

@end

@implementation PascalsTriangle

-(instancetype)initWithNumberOfRows:(NSUInteger)rows{
self = [super init];
if(self){
self.numberOfRows = rows;
self.rows = [NSArray array];
[self createTriangle];
}
return self;
}

-(void) createTriangle{

if(self.numberOfRows==0)
return;
NSMutableArray *triangle = [NSMutableArray array];
NSMutableArray *triangleRows = [NSMutableArray array];
[triangleRows addObject:[NSNumber numberWithInt:1]];
[triangle addObject:triangleRows];

self.rows = triangle;
if(self.numberOfRows==1)
return;

triangleRows = [NSMutableArray array];
[triangleRows addObject:[NSNumber numberWithInt:1]];
[triangleRows addObject:[NSNumber numberWithInt:1]];
[triangle addObject:triangleRows];

for(int i=2;i<self.numberOfRows;i++){
triangleRows = [NSMutableArray array];
[triangleRows addObject:[NSNumber numberWithInt:1]];
for(int j=1;j<[triangle[i-1] count];j++){
int value = [triangle[i-1][j] intValue] + [triangle[i-1][j-1] intValue];
[triangleRows addObject:[NSNumber numberWithInt:value]];
}
[triangleRows addObject:[NSNumber numberWithInt:1]];
[triangle addObject:triangleRows];

}

self.rows = triangle;

}

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