Skip to content

Instantly share code, notes, and snippets.

@andycroll
Created April 8, 2010 03:17
Show Gist options
  • Save andycroll/359736 to your computer and use it in GitHub Desktop.
Save andycroll/359736 to your computer and use it in GitHub Desktop.
require 'benchmark'
require 'pp'
def build_fixtures(n_teams, play_each_other=1)
teams = (1..n_teams).to_a
n_rounds = n_teams - 1 + n_teams%2
fixtures_per_round = n_teams / 2
# puts "Teams : #{teams.inspect}"
# puts "Rounds : #{n_rounds}"
# puts "Games per : #{fixtures_per_round}"
bottom = teams.pop(n_teams/2)
key = bottom.pop
top = teams.reverse
set = (0..(n_rounds-1)).map do |round|
top.push(bottom.pop)
bottom.unshift(top.shift)
# puts "--"
# puts "(#{top*','})"
# puts "(#{bottom*','})#{key}"
# puts "--"
(0..(fixtures_per_round-1)).map do |f|
round%2==0 ? [top[f], bottom[f]||key] : [bottom[f]||key, top[f]]
end
end
if play_each_other > 1
reverse_set = set.map { |x| x.map { |y| y.reverse }}
(1..play_each_other).inject([]) do |rounds, l|
rounds += (l%2==0 ? set : reverse_set )
end
else
set
end
end
final = []
Benchmark.bm do |x|
x.report("5 teams") { (1..10000).each { |n| final = build_fixtures(5) } }
x.report("5 teams (2x)") { (1..10000).each { |n| final = build_fixtures(5,2) } }
x.report("10 teams") { (1..10000).each { |n| final = build_fixtures(10) } }
x.report("10 teams (4x)") { (1..10000).each { |n| final = build_fixtures(10,4) } }
x.report("15 teams") { (1..10000).each { |n| final = build_fixtures(15) } }
x.report("20 teams") { (1..10000).each { |n| final = build_fixtures(20) } }
x.report("25 teams") { (1..10000).each { |n| final = build_fixtures(25) } }
end
final
puts "--"
pp build_fixtures(4,2)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment