4

I would like to write a League Fixture generator in python, but I can't. Here is the details:

There is a dynamic list of teams like teams = ["Team1", "Team2", "Team3", "Team4"]. How can I generate a fixture_weekx list from the teams list? For example:

fixture_week1 = ["Team1", "Team2", "Team3", "Team4"]
fixture_week2 = ["Team1", "Team3", "Team2", "Team4"]
fixture_week2 = ["Team1", "Team4", "Team2", "Team3"]

#Return matches:
fixture_week1 = ["Team2", "Team1", "Team4", "Team3"]
fixture_week2 = ["Team3", "Team1", "Team4", "Team2"]
fixture_week2 = ["Team4", "Team1", "Team3", "Team2"]

Any idea?

Roberto
  • 167
  • 1
  • 3
  • 19

3 Answers3

9

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')]
Maria Zverina
  • 10,863
  • 3
  • 44
  • 61
  • But the above solution is better for me, because it isn't include return matches. – Roberto Jun 28 '12 at 15:14
  • Return matches are easy ... just run the code with teams swapped around. I've added it to the demo - and made the fixture generation even faster!! :) – Maria Zverina Jun 29 '12 at 09:00
  • 1
    @MariaZverina i had a similar problem and this solved it perfectly. thank you – Inbar Rose Aug 08 '12 at 09:11
  • 7
    This doesn't work as explained. The output you show is not the actual output. Additionally, In the real output, Team 1 has one day off, Team 2 has two days off, Team 3 have no days off, Team 4 has no days off and team 5 has two days off. In the "Return matches" teams o 1 and 4 both don't get a day off while other have multiples. – Johnny Craig Sep 02 '15 at 18:19
  • 1
    Just in case anyone finds this question- @JohnnyCraig is right. This code does not output a true round robin schedule. – dkhaupt Sep 15 '16 at 15:16
  • this doesn't work. add a 6th team and you will see it doesn't properly round-robin the schedule. – azuras Mar 26 '17 at 17:41
  • Doesn't produce true round-robin. Please downvote this answer. – Scrontch Mar 07 '18 at 14:56
  • Doesn't work in Python3 – Luigi Plinge Mar 23 '20 at 09:53
6

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]))
jjk
  • 61
  • 1
  • 3
5

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')]