问题描述:

I am trying to create a fixture list for a season based on the ids of teams. There are 20 teams, and each week, any one team can only play once. So, for week one, the fixtures would be (1, 2), (3, 4), (5, 6), ..., (19, 20). Then week two, (1, 3), (2, 4), (5, 7), ..., (18, 20).

Is there some sort of a formula I could do that would work the fixtures out easily? Maybe combinations are not the best thing to use here. What would be the best way about going to solve this problem?

`class FixtutreGenerator`

a = Array(1..20)

i = 0

while i < a.combination(2).to_a.length

print a.combination(2).to_a[i]

i = i + 20

end

end

It sounds like you are trying to do the scheduling of teams in a round-robin tournament. Wikipedia describes an algorithm you could use - see http://en.wikipedia.org/wiki/Round-robin_tournament#Scheduling_algorithm.

The following implements the algorithm to print each week's pairings:

```
teams = Array(1..20)
fixed_team = teams.shift #The fixed competitor described in the algorithm
teams.length.times do |i|
#Create the two groups listed in the algorithm
teams = teams.rotate
week_teams = teams.dup.unshift(fixed_team)
first_group, second_group = week_teams.each_slice(week_teams.length/2).to_a
second_group.reverse!
weeks_pairings = first_group.zip(second_group)
#Output the week's pairings
puts "Week #{i + 1}: #{weeks_pairings}"
end
#Output:
#=> Week 1: [[1, 2], [3, 20], [4, 19], [5, 18], [6, 17], [7, 16], [8, 15], [9, 14], [10, 13], [11, 12]]
#=> Week 2: [[1, 3], [4, 2], [5, 20], [6, 19], [7, 18], [8, 17], [9, 16], [10, 15], [11, 14], [12, 13]]
#=> etc
```