Engaged Employer
NestedInteger weight sum - given a class with callable methods isInteger, getInteger, and getList get the weighted sum, e.g. {1,{4,{6}}} => 1 + (4*2) + (6*3) = 27
Anonymous
- (NSNumber*)depthSum:(NSArray *)input { return [self depthSum:input atDepth:@(1)]; } - (NSNumber*)depthSum:(NSArray *)input atDepth:(NSNumber*)depth { NSInteger sum = 0; for (NestedInteger *n in input) { if ([n isInteger]) { sum += [depth integerValue]*[n getInteger]; } else { sum += [depth integerValue]*[self depthSum:[n getList]] atDepth:@([depth integerValue]+1)]; } } return @(sum); }
Check out your Company Bowl for anonymous work chats.