# league problem in python

Andrew Henshaw andrew.henshaw at gtri.gatech.edu
Thu Apr 2 20:29:50 CEST 2009

```"Ross" <ross.jett at gmail.com> wrote in message
... snip ...
> I would like to create a simple program where the pro could enter in
> how many people were in the league, the number of courts available,
> and the number of weeks the schedule would run and then generate a
> schedule where everybody played everybody else once and got the same
> number of bye weeks, preferably spaced out evenly.
>
> How should I go about starting this problem...I'm feel like this is a
> really simple problem, but I'm having writer's/coder's block. Can you
> guys help?

At least as a start, you want the round-robin tournament algorithm (see
http://en.wikipedia.org/wiki/Round-robin_tournament).  Here's some code that
I use:

###### begin code  #######
def roundrobin(teams, rounds=1):
# if odd number of teams, add a "bye" team
if len(teams) % 2:
teams.append(None)
mid = len(teams) // 2
for i in range(rounds):
yield zip(teams[:mid], teams[mid:])
teams = teams[0:1] + teams[mid:mid+1] + teams[1:mid-1] +
teams[mid+1:] + teams[mid-1:mid]

### test code ###
if __name__ == '__main__':
ROUNDS = 10
# two test cases - even number of teams and odd number of teams
for teams in  (range(6), range(5)):
print "\nteams =", teams
for round in roundrobin(teams, ROUNDS):
print round

###### end code  #######

And the output:

teams = [0, 1, 2, 3, 4, 5]
[(0, 3), (1, 4), (2, 5)]
[(0, 4), (3, 5), (1, 2)]
[(0, 5), (4, 2), (3, 1)]
[(0, 2), (5, 1), (4, 3)]
[(0, 1), (2, 3), (5, 4)]
[(0, 3), (1, 4), (2, 5)]
[(0, 4), (3, 5), (1, 2)]
[(0, 5), (4, 2), (3, 1)]
[(0, 2), (5, 1), (4, 3)]
[(0, 1), (2, 3), (5, 4)]

teams = [0, 1, 2, 3, 4]
[(0, 3), (1, 4), (2, None)]
[(0, 4), (3, None), (1, 2)]
[(0, None), (4, 2), (3, 1)]
[(0, 2), (None, 1), (4, 3)]
[(0, 1), (2, 3), (None, 4)]
[(0, 3), (1, 4), (2, None)]
[(0, 4), (3, None), (1, 2)]
[(0, None), (4, 2), (3, 1)]
[(0, 2), (None, 1), (4, 3)]
[(0, 1), (2, 3), (None, 4)]

--
Andy

```