sharing/swapping items between lists

Ross ross.jett at gmail.com
Tue Apr 14 22:45:15 EDT 2009


On Apr 14, 7:18 pm, Aaron Brady <castiro... at gmail.com> wrote:
> On Apr 14, 7:01 pm, Aaron Brady <castiro... at gmail.com> wrote:
>
>
>
> > On Apr 14, 12:37 pm, Ross <ross.j... at gmail.com> wrote:
>
> > > On Apr 14, 10:34 am, Ross <ross.j... at gmail.com> wrote:
>
> > > > On Apr 14, 5:57 am, a... at pythoncraft.com (Aahz) wrote:
>
> > > > > In article <f64c9de2-3285-4f74-adb8-2111c78b7... at 37g2000yqp.googlegroups.com>,
>
> > > > > Ross  <ross.j... at gmail.com> wrote:
> > > > > >On Apr 13, 9:08=A0am, a... at pythoncraft.com (Aahz) wrote:
> > > > > >> In article <c569228f-f391-4317-83a2-08621c601... at r8g2000yql.googlegroups.=
> > > > > >com>,
> > > > > >> Ross =A0<ross.j... at gmail.com> wrote:
>
> > > > > >>>I'm sorry...my example was probably a bad one. A better example of
> > > > > >>>output I would like would be something like [[1,2],[3,4],[5,6]] and
> > > > > >>>then for the leftovers list [7,8,9,10 etc]. What I'm trying to do is
> > > > > >>>produce some sort of round robin algorithm for tennis that is
> > > > > >>>constrained by the number of courts available each week. So if there
> > > > > >>>are only 3 courts available for a singles league and 10 people have
> > > > > >>>signed up, 4 players will have a bye each week. I want my algorithm to
> > > > > >>>produce unique matchups each week and also give each player the same
> > > > > >>>angle?
>
> > > > > >> How about Googling for "round robin algorithm python"? ;-)
>
> > > > > >I have the basic algorithm and it works fine...I'm just having trouble
> > > > > >adding another parameter to it that allows for court constraints and
> > > > > >bye weeks.
>
> > > > > You'll need to give us more information, then.  Why don't you start with
> > > > > the core algorithm you're using?
> > > > > --
> > > > > Aahz (a... at pythoncraft.com)           <*>        http://www.pythoncraft.com/
>
> > > > > Why is this newsgroup different from all other newsgroups?
>
> > > > Here's the core algorithm I'm using:
>
> > > > >>> def round_robin(teams,rounds):
>
> > > >         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]
>
> > > > >>> if __name__== '__main__':
>
> > > >         rounds = 15
> > > >         teams = range(16)
> > > >         for round in round_robin(teams,rounds):
> > > >                 print round
>
> > > fyi rounds=15 and teams =range(16) was just test code I was playing
> > > around with...they could theoretically be anything.
>
> > Here is an idea.  Create a list of all possible pairs, using
> > itertools.combinations.  You'll notice everyone gets equal play time
> > and equal time against each other on a pair-by-pair basis.  Then, call
> > random.shuffle until one player isn't playing on two courts in one
> > day.
>
> This might take a long time.  Not that I can guarantee that a depth-
> first-search would be any faster, or that a breadth-first-search would
> run faster *and* run in available memory.  <cough>

I have a sub-optimal solution that I'm playing with now. Since my
output is a list of tuples and looks something like this (if there
were 16 teams and 15 rounds), I could designate a each nth tuple in
each round as a bye, but since the 1st item in my list remains fixed,
it's suboptimal. For example, you could say every 4th (and/or 3rd ,
5th, etc depending on how many available cts) tuple in each round gets
a bye and pop() it from the list...:

[(0, 8), (1, 9), (2, 10), (3, 11), (4, 12), (5, 13), (6, 14), (7, 15)]
[(0, 9), (8, 10), (1, 11), (2, 12), (3, 13), (4, 14), (5, 15), (6, 7)]
[(0, 10), (9, 11), (8, 12), (1, 13), (2, 14), (3, 15), (4, 7), (5, 6)]
[(0, 11), (10, 12), (9, 13), (8, 14), (1, 15), (2, 7), (3, 6), (4, 5)]
[(0, 12), (11, 13), (10, 14), (9, 15), (8, 7), (1, 6), (2, 5), (3, 4)]
[(0, 13), (12, 14), (11, 15), (10, 7), (9, 6), (8, 5), (1, 4), (2, 3)]
[(0, 14), (13, 15), (12, 7), (11, 6), (10, 5), (9, 4), (8, 3), (1, 2)]
[(0, 15), (14, 7), (13, 6), (12, 5), (11, 4), (10, 3), (9, 2), (8, 1)]
[(0, 7), (15, 6), (14, 5), (13, 4), (12, 3), (11, 2), (10, 1), (9, 8)]
[(0, 6), (7, 5), (15, 4), (14, 3), (13, 2), (12, 1), (11, 8), (10, 9)]
[(0, 5), (6, 4), (7, 3), (15, 2), (14, 1), (13, 8), (12, 9), (11, 10)]
[(0, 4), (5, 3), (6, 2), (7, 1), (15, 8), (14, 9), (13, 10), (12, 11)]
[(0, 3), (4, 2), (5, 1), (6, 8), (7, 9), (15, 10), (14, 11), (13, 12)]
[(0, 2), (3, 1), (4, 8), (5, 9), (6, 10), (7, 11), (15, 12), (14, 13)]
[(0, 1), (2, 8), (3, 9), (4, 10), (5, 11), (6, 12), (7, 13), (15, 14)]



More information about the Python-list mailing list