Esempio n. 1
0
function getSlate($tallies, $slate, $seats)
{
    if (count($slate) >= $seats) {
        return $slate;
    }
    arsort($tallies);
    $highest = max($tallies);
    $winners = array();
    foreach ($tallies as $cand => $score) {
        if ($highest == $score) {
            $slate[] = $cand;
            unset($tallies[$cand]);
        }
    }
    return getSlate($tallies, $slate, $seats);
}
function getSlate($tallies, $slate, $seats)
{
    if (count($slate) >= $seats) {
        return $slate;
    }
    arsort($tallies);
    $highest = max($tallies);
    $winners = array();
    $current_top_set = array();
    foreach ($tallies as $cand => $score) {
        if ($highest == $score) {
            $current_top_set[] = $cand;
        }
    }
    if (count($slate) + count($current_top_set) > $seats) {
        $num_needed = $seats - count($slate);
        //shuffle($current_top_set);
        //$to_add = array_slice($current_top_set,0,$num_needed);
        $percentage = round(100 * ($num_needed / count($current_top_set)), 5);
        foreach ($current_top_set as $chosen) {
            $slate[$chosen] = $percentage;
            unset($tallies[$chosen]);
        }
    } else {
        foreach ($current_top_set as $chosen) {
            $slate[$chosen] = 100;
            unset($tallies[$chosen]);
        }
    }
    return getSlate($tallies, $slate, $seats);
}