Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

League fixture generator in python [duplicate]

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?

like image 884
Roberto Avatar asked Dec 04 '22 15:12

Roberto


2 Answers

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')]
like image 137
Maria Zverina Avatar answered Dec 07 '22 05:12

Maria Zverina


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]))
like image 35
jjk Avatar answered Dec 07 '22 05:12

jjk