Created
December 11, 2018 14:42
-
-
Save betzerra/71607cc8d82315e4d64c4bae08a349b5 to your computer and use it in GitHub Desktop.
secret_santa.rb
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
people = ARGV | |
possible_receivers = people.dup | |
result = {} | |
people.each do |p| | |
# a person doesn't have to give a gift to himself; | |
tmp = possible_receivers.dup | |
tmp.delete(p) | |
receiver = tmp.sample | |
# a person doesn't receive a gift from the person he's given a gift to; | |
while result[receiver].nil? == false && result[receiver] == p do | |
receiver = tmp.sample | |
end | |
result[p] = receiver | |
possible_receivers.delete(receiver) | |
end | |
puts result |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment