Skip to content

Instantly share code, notes, and snippets.

@dshimy
Created March 30, 2016 05:41
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dshimy/1d680567e498cd65f56c6cda18eff4af to your computer and use it in GitHub Desktop.
Save dshimy/1d680567e498cd65f56c6cda18eff4af to your computer and use it in GitHub Desktop.
Gerber Schedule (Naturally balanced)
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