Skip to content

Instantly share code, notes, and snippets.

@orkoden
Last active August 29, 2015 14:22
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save orkoden/a04cb40f3997e38dd62f to your computer and use it in GitHub Desktop.
Save orkoden/a04cb40f3997e38dd62f to your computer and use it in GitHub Desktop.
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