Giter VIP home page Giter VIP logo

cs50-php's People

Contributors

erikccoder avatar

Watchers

 avatar  avatar

cs50-php's Issues

runoff - vote

function vote($voter, $rank, $name)
{    
    global $candidates, $preferences;
    $num_candidates = count($candidates);    
    for($i=0; $i<$num_candidates; $i++)
    {
        if($candidates[$i]['name'] === $name)
        {
            $preferences[$voter][$rank] = $i;
        }        
    }
}

runoff - print_winner

function print_winner()
{
    global $candidates, $preferences;
    $num_voters = count($preferences);
    $votes_to_win = ceil($num_voters*.5);
    foreach($candidates as $candidate)
    {
        if($candidate['vote'] > $votes_to_win)
        {
            printf($candidate['name'] . "\n");
            return 1;
        }
    }
    return 0;
}

runoff - find_min

function find_min()
{
    global $candidates, $preferences;
    $min_vote = 999999;
    foreach( $candidates as $candidate)
    {
        if(!$candidate['eliminated']){
            $vote = $candidate['vote'];
            if($min_vote > $vote)
            {
                $min_vote = $vote;   
            }            
        }
    }
    return $min_vote;
}

runoff - eliminate

function eliminate($min)
{
    global $candidates;
    $num_candidates = count($candidates);    
    for($i=0; $i<$num_candidates; $i++)
    {
        if(!$candidates[$i]['eliminated'])
        {
            if($candidates[$i]['vote'] <= $min){
                $candidates[$i]['eliminated'] = 1;
            }
        }
    }
}

runoff - main

$candidates = [];
for($i=1; $i<$argc; $i++)
{
    $candidates[] = [
        'name' => $argv[$i],
        'vote' => 0,
        'eliminated' => 0,
    ];
}

$preferences = [];

$num_voters = -1;

do{
    $num_voters = get_int("Number of voters: ");
    // print_r([
    //     '$num_voters' => $num_voters,
    // ]);
}
while($num_voters <= 0);

$preferences = [];
for($j=0; $j<$num_voters; $j++)
{
    $pref = [];
    for($i=1; $i<$argc; $i++)
    {
        $pref[] = -1;
    }
    $preferences[] = $pref;
}


$num_input = $num_voters * ($argc-1);
$current_input = 0;


while($num_input >= $current_input + 1)
{
    $i = ($current_input % ($argc-1)) + 1;
    
    $name = get_string("Rank $i: ");
    vote(floor(($current_input)/3), floor($i-1), $name);
    $current_input++;
}

$stage = 0;
while($stage < $argc - 1 )
{    
    tabulate();
    $has_winner = print_winner();
    if(!$has_winner)
    {
        $min = find_min();
        $is_tie = is_tie($min);
        if($is_tie)
        {
            $stage = 999;                        
        }
        eliminate($min);
    }
    $stage += $has_winner ? 999 : 1;     
}

runoff - tabulate

function setPreference($pref, $stage)
{
    global $candidates;
    $c_index = $pref[$stage];
    if(isset($c_index))
    {
        $candidate = $candidates[$c_index];
        if(isset($candidate))
        {
            if($candidate['eliminated'])
            {
                setPreference($pref, $stage+1);
            }
            else
            {
                $candidates[$c_index]['vote'] += 1;
            }
        }
    }
}

function tabulate()
{
    global $candidates, $preferences;
    $num_candidates = count($candidates);    
    for($i=0; $i<$num_candidates; $i++)
    {
        $candidates[$i]['vote'] = 0;
    }

    foreach($preferences as $pref)
    {   
        setPreference($pref, 0);
    }
}

runoff - is_tie

function is_tie($min)
{
    global $candidates;
    $votes_to_check = -1;
    foreach( $candidates as $candidate)
    {
        if(!$candidate['eliminated'])
        {
            $vote = $candidate['vote'];            
            if($votes_to_check === -1)
            {
                $votes_to_check = $vote;
            }
            else if($votes_to_check != $vote)
            {
                return false;
            } 
        }
    }
    if($votes_to_check == -1){
        print_r([
            'error' => "votes_to_check: $votes_to_check which all candidates are eliminated"
        ]);
    }
    return true;
}

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.