★ wanayoo — archive 1999 http://mail.python.org/pipermail/tutor/2001-June/006643.htmlNouvelle recherche | Portail wanayoo

[Tutor] A (not so) simple question? Round Robin

Roland Schlenker rol9999@attglobal.net
Tue, 19 Jun 2001 19:39:07 -0500


Christopher Smith wrote:
> 
> Dear Greg,
> 
> I'm learning Python and this was a good learning exercise.
> 
> I, too, found the web site that you did and implemented
> a solution using two lists and a dictionary.  Basically
> the dictionary acts as a matrix which is accessed through
> a tuple pair. The lists are the two lines of participants;
> for a given round, the competitors across from each other
> have their entry in the dictionary filled with the round
> number.  I modified your print-out routine to fit with
> my data.  You can probably tell that I am use to printing
> out matrices and don't yet know a more Python like way to
> print out an entire row of my dictionary inone statement.
> Basically I need to do something like "print all dictionary
> entries that have i as the first member of the tuple key."
> Can anyone provide any tips on this?

I think the way you did is just fine.

If you are asking how to find all entries that have i as the
first member of the tuple key:

for k in dict.keys():
    if k[0] == i:
       do something with k


> 
> /c
> 
> Code follows:
> 
> def roundrobin(n):
>  # Uses the "rotating lines" scheme presented
>  # at http://www.rain.org/~mkummel/stumpers/09feb01a.html
>  # to print a table for a round robin tournament
>  # between n teams which are represented by capital letters.
>  # If n is odd, the "bye" is indicated with a "_".
> 
>  # make lines
>   l=[];m=[];t={};odd=0
>   if n/2*2 !=n:
>     n=n+1
>     odd=1
>   n2=n/2
>   for i in range(n2):
>     l.append(i)
>     m.append(i+n2)
> 
>  # do the rounds
>   for r in range(n-1):
>     for i in range(n2):
>       t[(l[i],m[i])],t[(m[i],l[i])]=r,r
> 
>     # rotate the lines (this was fun!)
>     l.append(m.pop())
>     m.insert(0,l.pop(1))
> 
>  # define team names and print results
>   teams = map(chr,range(65,65+n))
>   if odd:
>     teams[-1]="_"
>   print "  |" + "%4s"*n % tuple(teams)
>   print "--|" + "----"*n
>   for i in range(n):
>     print "%s |" % teams[i],
>     for j in range(n):
>       if i==j:
>         t[(i,i)]=-1 # initialize diagonal
>       # Without the () in the next line, a string+int error is obtained
>       # Also, the trailing comma leaves a space between elements so the
>       # total space is actually 4, not 3, for each round number
>       print "%3d" % (t[(i,j)]+1),
>     print
> 
> roundrobin(7)
> 
> #  This is the output
> #
> #    |   A   B   C   D   E   F   G   _
> #  --|--------------------------------
> #  A |   0   2   3   4   1   7   6   5
> #  B |   2   0   6   3   5   1   4   7
> #  C |   3   6   0   7   2   5   1   4
> #  D |   4   3   7   0   6   2   5   1
> #  E |   1   5   2   6   0   4   7   3
> #  F |   7   1   5   2   4   0   3   6
> #  G |   6   4   1   5   7   3   0   2
> #  _ |   5   7   4   1   3   6   2   0
> 
> _______________________________________________
> Tutor maillist  -  Tutor@python.org
> http://mail.python.org/mailman/listinfo/tutor

Roland Schlenker