Hacker News new | ask | show | jobs
by pandaexpress 4894 days ago
I actually had a bit of trouble with this problem, it took a while to iron out the bugs. I copied down the triangle for reference but accidentally had 1 2 2 1 as the third row.

Took me about 15 minutes to do. Doesn't seem like an unreasonable question to ask.

1 comments

The solution (in Objective-C)

- (NSArray)pascal:(int)n { if (n == 1) return @[@(1)];

NSArray array = [self pascal:n-1]; NSMutableArray* mArray = [@[] mutableCopy]; for(int i = 0; i < array.count+1; i++) { int r = i - 1; if (r >= 0 && i < array.count) [mArray addObject:@([array[r] integerValue] + [array[i] integerValue])]; else if (i < array.count) [mArray addObject:@(0 + [array[i] integerValue])]; else [mArray addObject:@(0 + [array[i-1] integerValue])]; }

return [mArray copy]; }