Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl-Sensitive Sunglasses
 
PerlMonks  

Re: Algorithm problem: Order matches by difference between players

by moritz (Cardinal)
on Apr 18, 2011 at 15:50 UTC ( [id://899968]=note: print w/replies, xml ) Need Help??


in reply to Algorithm problem: Order matches by difference between players

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.

  • Comment on Re: Algorithm problem: Order matches by difference between players
  • Download Code

Replies are listed 'Best First'.
Re^2: Algorithm problem: Order matches by difference between players
by dwm042 (Priest) on Apr 18, 2011 at 16:32 UTC
    On odd sizes, if you allow for the person whose "#", so to speak, has turned up to have a 'bye', then I suspect it works pretty well.

    In other words, if player 2 in a 7 player tournament has a bye in round 5, player 3 a bye in round 3 and player 4 a bye in round 1, and writing code to allow byes in odd sized tourneys, the same kind of logic as in the even case should fit.

    This chart might explain it better:

    1: 7-6-5-4-3-2
    2: 6-5-4-3-B-1
    3: 5-4-B-2-1-7
    4: B-3-2-1-7-6
    5: 3-2-1-7-6-B
    6: 2-1-7-B-5-4
    7: 1-B-6-5-4-3


    David.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://899968]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others having a coffee break in the Monastery: (2)
As of 2024-04-26 01:29 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found