Created
March 30, 2016 05:41
-
-
Save dshimy/1d680567e498cd65f56c6cda18eff4af to your computer and use it in GitHub Desktop.
Gerber Schedule (Naturally balanced)
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
def generate_berger_schedule(teams: 8, rounds: 1) | |
matches = [] | |
team_sequence = (1..(teams - 1)).to_a | |
(1..rounds).each do |round| | |
team_sequence_for_round = [] | |
games = [] | |
if round.even? | |
team_sequence_for_round = Array.new(team_sequence) | |
games << [teams, team_sequence_for_round.shift] | |
(0..(teams/2 - 2)).each do |i| | |
games << [team_sequence_for_round[i], team_sequence_for_round[(i * -1) - 1]] | |
end | |
else | |
team_sequence_for_round = team_sequence + [teams] | |
(0..(teams/2 - 1)).each do |i| | |
games << [team_sequence_for_round[i], team_sequence_for_round[(i * -1) - 1]] | |
end | |
end | |
team_sequence = team_sequence.map do |i| | |
if (i + (teams / 2)) > (teams - 1) | |
i - ((teams / 2) - 1) | |
else | |
i + (teams / 2) | |
end | |
end | |
matches << games | |
end | |
matches | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment