Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Fizzbuzz in Objective-C that works without if, modulo, lookup table
#import <Foundation/Foundation.h>
NSMutableArray* fizzbuzzreplace(NSMutableArray* numberArray, NSUInteger divider, NSString* replacementString)
{
for (NSUInteger i = divider; i < numberArray.count + 1; i = i + divider) {
[numberArray replaceObjectAtIndex:i - 1 withObject:replacementString];
}
return numberArray;
}
int main(int argc, const char * argv[]) {
@autoreleasepool {
NSMutableArray* numbersTo100 = [NSMutableArray arrayWithCapacity:100];
for (NSUInteger i = 1; i < 101; ++i) {
[numbersTo100 addObject:[NSNumber numberWithUnsignedInteger:i]];
}
fizzbuzzreplace(fizzbuzzreplace(fizzbuzzreplace(numbersTo100, 3, @"Fizz"), 5, @"Buzz"), 15, @"FizzBuzz");
NSLog(@"%@", [numbersTo100 description]);
}
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.