Code works fine except...

Ross ross.jett at gmail.com
Tue May 5 23:37:37 EDT 2009


On May 5, 10:33 am, MRAB <goo... at mrabarnett.plus.com> wrote:
> Ross wrote:
> > On May 5, 12:32 am, John Yeung <gallium.arsen... at gmail.com> wrote:
> >> On May 5, 1:12 am, John Yeung <gallium.arsen... at gmail.com> wrote:
>
> >>> [...] the problem may require bigger guns (either much better
> >>> math or much more sophisticated programming).
> >> Yes, I'm responding to myself.
>
> >> Well, I went ahead with the approach I mentioned earlier, generating
> >> all possible matches and then selecting among them as needed to fill
> >> up the courts, trying to keep the number of matches played by each
> >> player as fair as possible.  (I should mention that this, or something
> >> similar, was suggested earlier by someone else in a different thread,
> >> in response to the same question by the same OP.)
>
> >> I did use "bigger guns" (mainly a class for player objects, with
> >> custom __cmp__ method), but still didn't do anything with doubles.
>
> >> I haven't tested it much, but I'll post it if anyone's interested.
> >> (That way people can pick on me instead of the OP. ;)
>
> >> John
>
> > I'm interested to see what you did. From your description, it sounds
> > like I've tried what you've done, but when I implemented my version,
> > it took minutes to evaluate for bigger numbers. If that isn't the case
> > with yours, I'd be interested in seeing your implementation.
>
> Here's my approach (incomplete):
>
> def get_pair(player_list, played):
>      for first in range(len(player_list)):
>          player_1 = player_list[first]
>          for second in range(first + 1, len(player_list)):
>              player_2 = player_list[second]
>              pair = player_1, player_2
>              sorted_pair = tuple(sorted(pair))
>              if sorted_pair not in played:
>                  played.add(sorted_pair)
>                  del player_list[second]
>                  del player_list[first]
>                  return pair
>      return None
>
> def round_robin(player_list, courts, played):
>      playing = []
>      for c in range(courts):
>          pair = get_pair(player_list, played)
>          if pair is None:
>              break
>          playing.append(pair)
>      byes = player_list[:]
>      player_list[:] = byes + [player for pair in playing for player in pair]
>      yield playing, byes
>
> def test_round_robin(players, rounds, courts, doubles=False):
>      player_list = range(players)
>      played = set()
>      for r in range(rounds):
>          for playing, byes in round_robin(player_list, courts, played):
>              print playing, byes

FYI... I was testing your code further and discovered a strange
outcome... when there are 16 people for 7 courts, every 7th round your
code produces 4 byes instead of the correct 2 byes.



More information about the Python-list mailing list