🎉 Exercism Research is now launched. Help Exercism, help science and have some fun at research.exercism.io 🎉
Avatar of ingemar

ingemar's solution

to Pascal's Triangle in the Objective-C Track

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

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

pascals-triangle-tests/PascalsTriangle.h

//
//  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

pascals-triangle-tests/PascalsTriangle.m

//
//  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

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?