# Code works fine except...

Chris Rebert clp2 at rebertia.com
Mon May 4 05:29:22 CEST 2009

```On Sun, May 3, 2009 at 7:36 PM, Ross <ross.jett at gmail.com> wrote:
> For the past couple weeks, I've been working on an algorithm to
> schedule tennis leagues given court constraints and league
> considerations (i.e. whether it's a singles or a doubles league). Here
> were my requirements when I was designing this algorithm:
>
> -Each player plays against a unique opponent each week.
> -Similarly, in a doubles league, each player plays with a unique
> partner each week.
> -Each player gets a fair number of bye weeks (i.e. the player with the
> most bye weeks will have no more than one bye week than the player
> with the least number of bye weeks)
>
> I'm very close to arriving at my desired solution, but I have one
> glaring flaw. When I have an even number of players sign up for my
> league and there are court constraints, my current algorithm gives the
> first player in my league a bye week every single week. I'll post my
> code below and see how you guys think I should add to/ amend my code.
>
> def round_robin(players, rounds):
>    if len(players)%2:
>        players.insert(0, None)
>    mid = len(players)//2
>    for i in range(rounds):
>        yield zip(players[:mid], players[mid:])
>        players = players[0:1] + players[mid:mid+1] + players[1:mid-1] +
> players[mid+1:] + players[mid-1:mid]
>
>
> def test_round_robin(players, rounds, courts, doubles = False):
>    players = range(players)
>    for week in round_robin(players,rounds,courts):
>            if doubles == True:
>                    doubles_week = len(week)/2.0
>                    byes = doubles_week - courts
>                    if byes == 0:
>                            bye_list = []
>                    else:
>                            bye_list = week[::int(round(1.072*(courts/byes)+1.08))]
>                    playing = [u for u in week if u not in bye_list]
>                    midd = len(playing)//2
>                    doub_sched = zip(playing[:midd], playing[midd:])
>                    print doub_sched, bye_list
>            else:
>                    byes = len(week)- courts
>                    if byes == 0:
>                            bye_list = []
>                    else:
>                            bye_list = week[::int(round(1.072*(courts/byes)+1.08))]
>                    playing = [u for u in week if u not in bye_list]
>                    print playing, bye_list

Probably not the cause of the problem, but where did the magic numbers
1.072 and 1.08 come from?

Cheers,
Chris
--
http://blog.rebertia.com

```