logo       

[SPOILER] My solution to the tournament scheduler quiz: msg#00020

lang.perl.qotw.discuss

Subject: [SPOILER] My solution to the tournament scheduler quiz

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";
}

Attachment: signature.asc
Description: Digital signature

<Prev in Thread] Current Thread [Next in Thread>
Google Custom Search

News | FAQ | advertise