Created
May 14, 2015 19:46
-
-
Save Jxrgxn/9ca90fc3098298a8863f to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#import <Foundation/Foundation.h> | |
/** | |
* Takes an array of people to be arranged for Secret Santa and returns a dictionary where each person is a key and | |
* the corresponding value for each person is the other person to whom they will give. | |
* | |
* @param inputArray Array of people playing Secret Santa | |
* | |
* @return The dictionary from giver (key) to recipient (value) | |
*/ | |
@interface NSArray (Random) | |
- (id)randomObject; | |
@end | |
@implementation NSArray (Random) | |
-(id)randomObject { | |
NSUInteger myCount = [self count]; | |
if (myCount) | |
return [self objectAtIndex:arc4random_uniform(myCount)]; | |
else | |
return nil; | |
} | |
@end | |
static NSDictionary *pairSecretSantas(NSArray *inputArray) | |
{ | |
NSMutableDictionary *pairedSantas = [NSMutableDictionary new]; | |
// Iterate over the input array and pair givers/receivers. | |
NSMutableArray *inputArrayM = [inputArray mutableCopy]; | |
for (NSString *f in inputArray) { | |
NSString *flinstoneChar = [inputArrayM randomObject]; | |
//NSString *flinstoneChar2 = [inputArray randomObject]; | |
if (![f isEqualToString:flinstoneChar]) | |
{ | |
[pairedSantas setObject:f forKey:flinstoneChar]; | |
[inputArrayM removeObject:flinstoneChar]; | |
} | |
else if ([f isEqualToString:flinstoneChar]){ | |
} | |
} | |
NSLog(@"%@", pairedSantas); | |
// No person can be a giver more than once | |
// No person can receive more than one present | |
return pairedSantas; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment