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

Published at Jul 22 2019
·
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
```

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

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.

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

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

```
#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
// pascals-triangle-tests
//
// Created by Ingemar Pertl on 18.07.19.
// Copyright Â© 2019 Ingemar Pertl. All rights reserved.
//
#import <Foundation/Foundation.h>
NS_ASSUME_NONNULL_BEGIN
@interface PascalsTriangle : NSObject
@property (nonatomic) NSArray *rows;
- (instancetype)initWithNumberOfRows:(int)numberOfRows;
@end
NS_ASSUME_NONNULL_END
```

```
//
// PascalsTriangle.m
// pascals-triangle-tests
//
// Created by Ingemar Pertl on 18.07.19.
// Copyright Â© 2019 Ingemar Pertl. All rights reserved.
//
#import "PascalsTriangle.h"
@interface PascalsTriangle ()
@property (nonatomic) int numberOfRows;
- (NSArray *)compute;
@end
@implementation PascalsTriangle
- (instancetype)initWithNumberOfRows:(int)numberOfRows {
self = [super init];
self.numberOfRows = numberOfRows;
self.rows = [self compute];
return self;
}
- (NSArray *) compute{
NSMutableArray *rows = [NSMutableArray new];
for (int i = 0; i < self.numberOfRows; i++) {
if(i == 0) {
rows[i] = @[@1];
}
else {
NSArray<NSNumber *> *lastRow = [rows lastObject];
NSMutableArray<NSNumber *> *newRow = [NSMutableArray new];
newRow[0] = @1;
for(int j = 1; j < lastRow.count; j++) {
if(j == 0) {
newRow[j] = @1;
}
else {
NSNumber *value = [NSNumber numberWithFloat:([lastRow[j - 1] floatValue] + [lastRow[j] floatValue])];
newRow[j] = value;
}
}
newRow[lastRow.count] = @1;
rows[i] = newRow;
}
}
return rows;
}
@end
```

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?

Level up your programming skills with 3,450 exercises across 52 languages, and insightful discussion with our volunteer team of welcoming mentors.
Exercism is
**100% free forever**.

## Community comments