Last active
August 29, 2015 14:08
-
-
Save danielbonnell/9473dbdb2b7484ad4b38 to your computer and use it in GitHub Desktop.
Round Robin Challenge
This file contains hidden or 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
def schedule_tournament(names) | |
matches = names.product(names) | |
rounds = [] | |
matches.sort!.each do |sub| | |
sub.sort! | |
matches.delete(sub) if sub[0] == sub[1] | |
end | |
matches.uniq! | |
until matches.length == 0 | |
list = names.sort | |
until list.length == 0 | |
round = [] | |
matches.each do |match| | |
if list.length % 2 == 0 && list.include?(match[0]) && list.include?(match[1]) | |
round << match | |
list.delete(match[0]) | |
list.delete(match[1]) | |
matches.delete(match) | |
elsif list.length % 2 != 0 && list.include?(match[0]) | |
round << match | |
list.delete(match[0]) | |
matches.delete(match) | |
end | |
rounds << round | |
end | |
end | |
end | |
p rounds.uniq | |
end | |
schedule_tournament(['Mrs. Featherbottom', 'Ice the Bounty Hunter', 'Gene Parmesan', 'Buster Bluth']) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment