League fixture generator in python [duplicate]
Fixture scheduling is a well known problem. This is python implementation of algorithm given at: http://en.wikipedia.org/wiki/Round-robin_tournament
# generation code - for cut and paste
import operator
def fixtures(teams):
if len(teams) % 2:
teams.append('Day off') # if team number is odd - use 'day off' as fake team
rotation = list(teams) # copy the list
fixtures = []
for i in range(0, len(teams)-1):
fixtures.append(rotation)
rotation = [rotation[0]] + [rotation[-1]] + rotation[1:-1]
return fixtures
# demo code
teams = ["Team1", "Team2", "Team3", "Team4", "Team5"]
# for one match each - use this block only
matches = fixtures(teams)
for f in matches:
print zip(*[iter(f)]*2)
# if you want return matches
reverse_teams = [list(x) for x in zip(teams[1::2], teams[::2])]
reverse_teams = reduce(operator.add, reverse_teams) # swap team1 with team2, and so on ....
#then run the fixtures again
matches = fixtures(reverse_teams)
print "return matches"
for f in matches:
print f
This generates output:
[('Team1', 'Day off'), ('Team2', 'Team5'), ('Team3', 'Team4')]
[('Team1', 'Team5'), ('Day off', 'Team4'), ('Team2', 'Team3')]
[('Team1', 'Team4'), ('Team5', 'Team3'), ('Day off', 'Team2')]
[('Team1', 'Team3'), ('Team4', 'Team2'), ('Team5', 'Day off')]
[('Team1', 'Team2'), ('Team3', 'Day off'), ('Team4', 'Team5')]
I wanted to comment that the code from @MariaZverina doesn't quite work. I tried it as is, but I didn't get the right pairings. The modification that I made below works with her code. The difference is that I do a rainbow style pairing of each fixture by zipping the first half of the fixture f with the reversed second half.
# demo code
teams = ["Team1", "Team2", "Team3", "Team4", "Team5"]
# for one match each - use this block only
matches = fixtures(teams)
for f in matches:
# This is where the difference is.
# I implemented "rainbow" style pairing from each fixture f
# In other words:
# [(f[0],[f[n-1]), (f[1],f[n-2]), ..., (f[n/2-1],f[n/2])],
# where n is the length of f
n = len(f)
print zip(f[0:n/2],reversed(f[n/2:n]))
The code from @MariaZverina hasn't worked, I have implemented this code using Round-robin_tournament as well:
teams = ["Team1", "Team2", "Team3", "Team4", "Team5", "Team6"]
if len(teams) % 2:
teams.append('Day off')
n = len(teams)
matchs = []
fixtures = []
return_matchs = []
for fixture in range(1, n):
for i in range(n/2):
matchs.append((teams[i], teams[n - 1 - i]))
return_matchs.append((teams[n - 1 - i], teams[i]))
teams.insert(1, teams.pop())
fixtures.insert(len(fixtures)/2, matchs)
fixtures.append(return_matchs)
matchs = []
return_matchs = []
for fixture in fixtures:
print fixture
Output:
[('Team1', 'Team6'), ('Team2', 'Team5'), ('Team3', 'Team4')]
[('Team1', 'Team5'), ('Team6', 'Team4'), ('Team2', 'Team3')]
[('Team1', 'Team4'), ('Team5', 'Team3'), ('Team6', 'Team2')]
[('Team1', 'Team3'), ('Team4', 'Team2'), ('Team5', 'Team6')]
[('Team1', 'Team2'), ('Team3', 'Team6'), ('Team4', 'Team5')]
[('Team6', 'Team1'), ('Team5', 'Team2'), ('Team4', 'Team3')]
[('Team5', 'Team1'), ('Team4', 'Team6'), ('Team3', 'Team2')]
[('Team4', 'Team1'), ('Team3', 'Team5'), ('Team2', 'Team6')]
[('Team3', 'Team1'), ('Team2', 'Team4'), ('Team6', 'Team5')]
[('Team2', 'Team1'), ('Team6', 'Team3'), ('Team5', 'Team4')]