Beefy Boxes and Bandwidth Generously Provided by pair Networks
Just another Perl shrine

Re: Round robin tournament problem

by dvergin (Monsignor)
on Oct 21, 2003 at 23:48 UTC ( #301112=note: print w/replies, xml ) Need Help??

in reply to Round robin tournament problem

Update: Drat! I suspect Limbic~Region is right about 2**n. This solution fails for groups of 6 and 10. But for groups numbering in powers of two read on.

Unencumbered as I am by any proper background in such things, let me offer this...

For each round, for each player in succession who does not yet have an opponent in that round, we pick the first available unmatched person who this player has not played in a previous round. For convenience in screening the possibilities, we keep track of the matchings both ways ( A vs B *and* B vs A). and then weed out the dupes later.

You may or may not find this a bit verbose. Season to taste:

#!/usr/bin/perl use warnings; use strict; { my @round_aoh; my @players = qw/John Jane Bill Suzi Bret Erin Eric Teri/; my $rounds = @players - 1; for my $round ( 1..$rounds ) { for my $player ( @players ) { next if $round_aoh[$round]{$player}; $round_aoh[$round]{$player} = 'pending'; my ($opponent) = grep {unmatched( $player, $_, \@round_aoh)} grep {not $round_aoh[$round]{$_}} @players; $round_aoh[$round]{$player } = $opponent; $round_aoh[$round]{$opponent} = $player; } } # Now, show what we did. for my $round ( 1..$rounds ) { print "Round $round\n"; for my $player ( keys %{$round_aoh[$round]} ) { # crude hack to skip dupes: A vs B : B vs A next unless $player gt $round_aoh[$round]{$player}; print " $player vs $round_aoh[$round]{$player}\n"; } } } sub unmatched { my ($player, $opponent, $round_arefoh) = @_; for my $round ( 1..@$round_arefoh) { if (defined $round_arefoh->[$round]{$player} and $round_arefoh->[$round]{$player} eq $opponent) { return 0; } } return 1; }
The rationalle seems right, the code runs, and the results look like they meet the spec. Good luck.


"Perl is a mess and that's good because the
problem space is also a mess.
" - Larry Wall

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://301112]
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others wandering the Monastery: (6)
As of 2017-05-25 09:05 GMT
Find Nodes?
    Voting Booth?