home *** CD-ROM | disk | FTP | other *** search
/ Source Code 1992 March / Source_Code_CD-ROM_Walnut_Creek_March_1992.iso / usenet / altsrcs / 1 / 1989 < prev    next >
Encoding:
Internet Message Format  |  1990-12-28  |  2.0 KB

  1. From: jpn@genrad.com (John P. Nelson)
  2. Newsgroups: alt.sources,alt.sources.wanted,comp.lang.perl
  3. Subject: Re: Round Robin Tournament playoff scheduler wanted
  4. Message-ID: <39222@genrad.UUCP>
  5. Date: 24 Oct 90 15:41:59 GMT
  6.  
  7. >Given a list of the names of the players in a tournament (such
  8. >as golf or chess) I'd like sources to a program which calculates
  9. >the number of rounds required and the pairs of players scheduled
  10. >to play during each round.
  11. >
  12. >CONDITIONS:
  13. >-   Every player must play every other player exactly once.
  14. >-   Every player must have an opponent during every round.
  15.  
  16. I took a shot at this, and implemented it using "perl".  Depending on
  17. how you invoke perl on your system, you may need to modify the first
  18. few lines of this script.
  19.  
  20. #! /usr/local/perl
  21. # roundrobin - take a list of player names, and create a roundrobin
  22. #              tournament where each player plays every other player
  23. #              exactly once, and each player is active each round.
  24.  
  25. # collect the names, one per line.
  26. $maxlen = 0;
  27. $phantom = "__phantom__";
  28. while (<>) {
  29.     chop;
  30.     push(players, $_);
  31.     if (length > $maxlen) { $maxlen = length; }
  32. }
  33.  
  34. die "Note enough players" if ($#players < 2);
  35. $format = sprintf("%%-%ds plays %%%ds\n", $maxlen, $maxlen);
  36.  
  37. # make sure we have an even number of players.
  38. if (($#players & 1) == 0) {
  39.     unshift(players, $phantom);
  40. }
  41.  
  42. # make rotator with two arrays and a pivot
  43. $pivot = $players[0];
  44. @list1 = @players[1..(($#players-1)/2)];
  45. @list2 = @players[(($#players+1)/2)..$#players];
  46.  
  47. # perform the round robin
  48. for ($i = 1; $i <= $#players; ++$i) {
  49.     print "\n Round $i:\n";
  50.     if ($pivot eq $phantom) { print "$list2[0] is idle\n"; }
  51.     else { printf $format, $pivot, $list2[0]; }
  52.  
  53.     for ($j = 0; $j <= $#list1; ++$j) {
  54.     printf $format, $list1[$j], $list2[$j+1];
  55.     }
  56.     # rotate the players
  57.     $temp = shift(list2);
  58.     unshift(list1, $temp);
  59.     $temp = pop(list1);
  60.     push(list2, $temp);
  61. }
  62.  
  63. ---
  64.      john nelson
  65.  
  66. uucp:    {decvax,mit-eddie}!genrad!jpn
  67. domain:    jpn@genrad.com
  68.