[ create a new paste ] login | about

Link: http://codepad.org/op9WcIih    [ raw code | output | fork ]

PHP, pasted on Sep 3:
<?php
    
    // Function generating order participants will be placed in array
    function getBracket($L) {
        // List will hold insert sequence
        $list = array();
        // Bracket will hold final order of participants
        $bracket = array();
        // The algorithm to generate the insert sequence
        for ($n = 1; $n <= $L; $n += 1) {
            // If 'perfect' number, just put it (Perfect no.s: 2, 4, 8, 16, 32, etc)
            if (substr(log($n)/log(2), -2) == ".0") {
                $list[] = $n;
            // If odd number, stuff...
            } elseif ($n % 2 == 1) {
                $list[] = $list[($n-1)/2];
            // Else even number, stuff...
            } else {
                $list[] = $list[$n/2-1]+$n/2;
            }
        }
        
        // Insert participant order as per insert sequence
        for ($i = 1; $i <= sizeof($list); $i += 1) {
            $id = $i-1;
            array_splice($bracket, $list[$id], 0, $i);
        }
        return $bracket;
    }
    
    // Find number of participants over 'perfect' number if any
    function cleanList($L) {
        for ($d = 1; $L > $d; $d += 1) {
            $sq = $L-pow(2,$d);
            if($sq == 0) {break;}
            if($sq < 0) {
                $d = pow(2,$d-1);
                $diff = $L-$d;
                break;
            }
        }
        return $diff;
    }
    
    $participants = array(
        array(0, "John", 2),
        array(1, "Gagan", 1),
        array(2, "Mike Tyson", 1),
        array(3, "Gair", 1),
        array(4, "Gale", 0),
        array(5, "Roy Johnes", 0),
        array(6, "Galip", 0),
        array(7, "Gallagher", 0),
        array(8, "Garett", 0),
        array(9, "Nikolai Valuev", 0),
        array(10, "Garner", 1),
        array(11, "Gary", 0),
        array(12, "Gelar", 0),
        array(13, "Gershom", 1),
        array(14, "Gilby", 0),
        array(15, "Gilford", 1),
        array(16, "Arianna", 0)
    );
    
    // Extract strength of participant
    foreach ($participants as $array) {
        $finorder[] = $array[2];
    }
    // Sort by strength, strongest first
    array_multisort($finorder,SORT_DESC,$participants);

    $order = array();
    $outside = array();

    // Remove participants above 'perfect' number
    $remove = cleanList(sizeof($participants));
    for ($r = 1; $r <= $remove; $r += 1) {
        $removed = array_shift($participants);
        $outside[] = $removed;
    }

    $nparticipants = array();
    for ($i = 1; $i <= sizeof($participants); $i += 1) {
        $par = array();
        $par = $participants[$i-1];
        array_splice($par, 0, 1, $i);
        $nparticipants[] = $par;
    }
    
    // Get corresponding bracket
    $res = getBracket(sizeof($nparticipants));
    foreach ($res as $n) {
        $order[] = $n;
    }

    // Align bracket results with participant list
    array_multisort($order, $nparticipants);

    
    echo "The final arrangement of participants\n";
    print_r($nparticipants);
    print_r($outside);
?>


Output:
The final arrangement of participants
Array
(
    [0] => Array
        (
            [0] => 1
            [1] => Gagan
            [2] => 1
        )

    [1] => Array
        (
            [0] => 9
            [1] => Galip
            [2] => 0
        )

    [2] => Array
        (
            [0] => 5
            [1] => Gershom
            [2] => 1
        )

    [3] => Array
        (
            [0] => 13
            [1] => Gary
            [2] => 0
        )

    [4] => Array
        (
            [0] => 3
            [1] => Gair
            [2] => 1
        )

    [5] => Array
        (
            [0] => 11
            [1] => Garett
            [2] => 0
        )

    [6] => Array
        (
            [0] => 7
            [1] => Gale
            [2] => 0
        )

    [7] => Array
        (
            [0] => 15
            [1] => Gilby
            [2] => 0
        )

    [8] => Array
        (
            [0] => 2
            [1] => Mike Tyson
            [2] => 1
        )

    [9] => Array
        (
            [0] => 10
            [1] => Gallagher
            [2] => 0
        )

    [10] => Array
        (
            [0] => 6
            [1] => Gilford
            [2] => 1
        )

    [11] => Array
        (
            [0] => 14
            [1] => Gelar
            [2] => 0
        )

    [12] => Array
        (
            [0] => 4
            [1] => Garner
            [2] => 1
        )

    [13] => Array
        (
            [0] => 12
            [1] => Nikolai Valuev
            [2] => 0
        )

    [14] => Array
        (
            [0] => 8
            [1] => Roy Johnes
            [2] => 0
        )

    [15] => Array
        (
            [0] => 16
            [1] => Arianna
            [2] => 0
        )

)
Array
(
    [0] => Array
        (
            [0] => 0
            [1] => John
            [2] => 2
        )

)


Create a new paste based on this one


Comments: