Beefy Boxes and Bandwidth Generously Provided by pair Networks
Pathologically Eclectic Rubbish Lister

comment on

( #3333=superdoc: print w/replies, xml ) Need Help??
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

In reply to Re: Round robin tournament problem by dvergin
in thread Round robin tournament problem by CiceroLove

Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":

  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.
  • Log In?

    What's my password?
    Create A New User
    and the web crawler heard nothing...

    How do I use this? | Other CB clients
    Other Users?
    Others pondering the Monastery: (2)
    As of 2021-02-28 04:35 GMT
    Find Nodes?
      Voting Booth?

      No recent polls found