Sha256: dba23a8979124bd9061dd6d1569ae06b9d4b1f28ea649b44d746042a84de687a

Contents?: true

Size: 1.38 KB

Versions: 250

Compression:

Stored size: 1.38 KB

Contents

#import "PascalsTriangleExample.h"

@implementation PascalsTriangle

- (instancetype)initWithNumberOfRows:(int)numberOfRows {
    self = [super init];
    
    if (self) {
        self.rows = [PascalsTriangle triangleForNumberOfRows:numberOfRows];
    }
    
    return self;
}

+ (NSMutableArray<NSArray<NSNumber *> *> *)triangleForNumberOfRows:(int)numberOfRows {
    NSMutableArray<NSArray<NSNumber *> *> *triangle = [[NSMutableArray alloc] init];
    
    for (int i = 0; i < numberOfRows; i++) {
        NSMutableArray *values = [[NSMutableArray alloc] init];
        
        if (i == 0) {
            [values addObject:@1];
        } else {
            NSArray<NSNumber *> *lastRow = triangle[i - 1];
            NSUInteger length = lastRow.count;
            
            for (int j = 0; j <= length; j++) {
                int left;
                
                if (j == 0) {
                    left = 0;
                } else {
                    left = [lastRow[j - 1] intValue];
                }
                
                int right;
                
                if (j < length) {
                    right = [lastRow[j] intValue];
                } else {
                    right = 0;
                }
                
                [values addObject:@(left + right)];
            }
        }
        
        [triangle addObject:values];
    }

    return triangle;
}

@end

Version data entries

250 entries across 250 versions & 1 rubygems

Version Path
trackler-2.2.1.98 tracks/objective-c/exercises/pascals-triangle/PascalsTriangleExample.m
trackler-2.2.1.97 tracks/objective-c/exercises/pascals-triangle/PascalsTriangleExample.m
trackler-2.2.1.96 tracks/objective-c/exercises/pascals-triangle/PascalsTriangleExample.m
trackler-2.2.1.95 tracks/objective-c/exercises/pascals-triangle/PascalsTriangleExample.m
trackler-2.2.1.94 tracks/objective-c/exercises/pascals-triangle/PascalsTriangleExample.m
trackler-2.2.1.93 tracks/objective-c/exercises/pascals-triangle/PascalsTriangleExample.m
trackler-2.2.1.92 tracks/objective-c/exercises/pascals-triangle/PascalsTriangleExample.m
trackler-2.2.1.91 tracks/objective-c/exercises/pascals-triangle/PascalsTriangleExample.m
trackler-2.2.1.90 tracks/objective-c/exercises/pascals-triangle/PascalsTriangleExample.m
trackler-2.2.1.89 tracks/objective-c/exercises/pascals-triangle/PascalsTriangleExample.m
trackler-2.2.1.88 tracks/objective-c/exercises/pascals-triangle/PascalsTriangleExample.m
trackler-2.2.1.87 tracks/objective-c/exercises/pascals-triangle/PascalsTriangleExample.m
trackler-2.2.1.86 tracks/objective-c/exercises/pascals-triangle/PascalsTriangleExample.m
trackler-2.2.1.85 tracks/objective-c/exercises/pascals-triangle/PascalsTriangleExample.m
trackler-2.2.1.84 tracks/objective-c/exercises/pascals-triangle/PascalsTriangleExample.m
trackler-2.2.1.83 tracks/objective-c/exercises/pascals-triangle/PascalsTriangleExample.m
trackler-2.2.1.82 tracks/objective-c/exercises/pascals-triangle/PascalsTriangleExample.m
trackler-2.2.1.81 tracks/objective-c/exercises/pascals-triangle/PascalsTriangleExample.m
trackler-2.2.1.80 tracks/objective-c/exercises/pascals-triangle/PascalsTriangleExample.m
trackler-2.2.1.79 tracks/objective-c/exercises/pascals-triangle/PascalsTriangleExample.m