Possible Duplicate:
Holiday Gift Exchange
Background: Secret Santa is a Western Christmas tradition in which members of a group or community are randomly assigned a person to whom they anonymously give a gift.
http://en.wikipedia.org/wiki/Secret_Santa
Ok, me and my work colleges did this today during our lunch hour - we made a secret algorithm santa pick random gifters and receivers.
Think of this question as a Christmas Programming Challenge to see who can come up with some of the most elegant solutions for this problem.
Input: The input should be an array ['Oliver', 'Paul', 'Rowan', 'Darren', 'Nick', 'Atif', 'Kevin']
Output A representation of something similar to key value pairs for the gifter and receiver. e.g.
Oliver -> Darren,
Paul -> Nick,
Rowan -> Kevin
Kevin -> Atif,
Darren -> Paul,
Nick -> Oliver,
Atif -> Rowan
Deadline: 15th December (for those late christmas shoppers)
Remember: you cannot have a person choosing themselves and the program must not spiral off into an infinite loop when gifter == receiver when there is only one person left.
Rules:
- Must not have duplicates
- Must be random (we all have different views on what is random - but you get my gist)
- No Language constraints
- Have fun
Here is mine (not golfed) in ruby:
require 'pp'
def move(array, from, to)
array.insert(to, array.delete_at(from))
end
gifters = ['Oliver', 'Paul', 'Rowan', 'Darren', 'Nick', 'Atif', 'Kevin'].shuffle!
recievers = gifters.dup
move recievers, recievers.count - 1, 0
pp Hash[gifters.zip recievers]
which spits out:
{"Nick"=>"Darren",
"Paul"=>"Nick",
"Kevin"=>"Paul",
"Rowan"=>"Kevin",
"Atif"=>"Rowan",
"Oliver"=>"Atif",
"Darren"=>"Oliver"}