This matches pretty well what I would do manually: start with player 1, and assign her to the worst player. Then proceed with the second best, and assign it to the second worst etc.

In the next iteration the best is assigned to the second-worst, thereby decreasing the "distance" by one. At each step I take a look if the "best choice" player is free, and if not, skip to next free player

use strict; use warnings; # perl 5.10 only used for say() and infix // use 5.010; my $size = shift(@ARGV) // 6; for my $turn (0.. ($size - 2)) { my @players; @players[0..($size-1)] = ((1) x $size); for (0 .. ($size / 2 - 1)) { my $current = $_; $current = ($current + 1) % $size until $players[$current]; $players[$current] = 0; my $other = ($size - $turn - $current - 1) % $size; $other = ($other - 1) % $size until $players[$other]; $players[$other] = 0; say $current + 1, '-', ($other + 1); } say '=' x 10; }

It seems to work for size 6, haven't tested it for odd sizes.

Update: I've just re-check the output and found that it repeats some pairings. While that might be fixable with a hash of already-seen pairs, I won't bother because there's a more elegant, working solution given in another reply.


In reply to Re: Algorithm problem: Order matches by difference between players by moritz
in thread Algorithm problem: Order matches by difference between players by Dirk80

Title:
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, details, 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, summary, 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.