[SPOILER] My solution to the tournament scheduler quiz
Walt Mankowski <[email protected]> Wed, 19 Jan 2005 11:40:34 -0500
| Newsgroups | gmane.comp.lang.perl.qotw.discuss |
|---|---|
| Message-ID | <[email protected]> |
I just coded up the algorithm given at
http://www.gamecabinet.com/rules/RoundRobinScheme.html
Walt
-----------------
#!/usr/local/bin/perl -w
use strict;
my $sched = allocate_schedule($ARGV[0]);
print_sched($sched);
sub allocate_schedule {
my $n = shift;
$n++ if $n % 2; # add ghost player
my @teams = (0..$n-1);
my @sched;
for (1..$n-1) {
my @round;
for my $j (0..$n-1) {
$round[$teams[$j]] = $teams[-($j+1)];
}
push @sched, \@round;
unshift @teams, splice @teams, -2, 1;
}
return \@sched;
}
sub print_sched {
my $sched = shift;
my $out = "[";
for my $round (@$sched) {
$out .= "[" . join(",", @$round) . '],';
}
chop $out; # remove final ,
$out .= "]";
print "$out\n";
}
signature.asc
(application/pgp-signature, 189 B)
-----BEGIN PGP SIGNATURE----- Version: GnuPG v1.2.4 (GNU/Linux) iD8DBQFB7o2CXfGeK2entYQRAgorAJ0dxxq5urw+Ulg72O0OG9XI1dEAJwCeNvWm 2jT5hQHYJxT98AHIj2cTrZI= =wHNT -----END PGP SIGNATURE-----