Skip to content

Instantly share code, notes, and snippets.

@tounsils
Last active February 12, 2022 06:43
Show Gist options
  • Save tounsils/d1453cb62111fa9413069d5c5196850d to your computer and use it in GitHub Desktop.
Save tounsils/d1453cb62111fa9413069d5c5196850d to your computer and use it in GitHub Desktop.
League Table
<?php
/**
* The LeagueTable class tracks the score of each player in a league. After each game, the player records their score with the recordResult function.
* The player's rank in the league is calculated using the following logic:
* The player with the highest score is ranked first (rank 1). The player with the lowest score is ranked last.
* If two players are tied on score, then the player who has played the fewest games is ranked higher.
* If two players are tied on score and number of games played, then the player who was first in the list of players is ranked higher.
* Implement the playerRank function that returns the player at the given rank.
* For example:
* $table = new LeagueTable(array('Mike', 'Chris', 'Arnold'));
* $table->recordResult('Mike', 2);
* $table->recordResult('Mike', 3);
* $table->recordResult('Arnold', 5);
* $table->recordResult('Chris', 5);
* echo $table->playerRank(1);
* All players have the same score. However, Arnold and Chris have played fewer games than Mike, and as Chris is before Arnold in the list of players, he is ranked first. Therefore, the code above should display "Chris".
*/
class LeagueTable
{
public function __construct($players)
{
$this->standings = array();
foreach($players as $index => $p)
{
$this->standings[$p] = array
(
'index' => $index,
'games_played' => 0,
'score' => 0,
'rank' => $index
);
}
}
public function recordResult($player, $score)
{
$this->standings[$player]['games_played']++;
$this->standings[$player]['score'] += $score;
}
function swap(&$x,&$y) {
$tmp=$x;
$x=$y;
$y=$tmp;
}
public function reRank()
{
//ranking all according to score
foreach($this->standings as $player => $records){
foreach($this->standings as $player1 => $records1){
if (($records['score'] > $records1['score']) &&
($records['rank'] >= $records1['rank']))
{$this->swap($this->standings[$player]['rank'],$this->standings[$player1]['rank']);}
// according to game played
if (($records['score'] == $records1['score']) &&
($records['games_played'] > $records1['games_played']))
{$this->swap($this->standings[$player]['rank'],$this->standings[$player1]['rank']);}
// according to index
if (($records['score'] == $records1['score']) &&
($records['games_played'] == $records1['games_played'])&&
($records['index'] > $records1['index']))
{$this->swap($this->standings[$player]['rank'],$this->standings[$player1]['rank']);}
}
}
}
public function playerRank($rank)
{
$this->reRank();
foreach($this->standings as $player => $records){
if ($records['rank']==$rank-1)
return $player;
}
}
}
$table = new LeagueTable(array('Chris', 'Mike', 'Arnold'));
$table->recordResult('Mike', 2);
$table->recordResult('Mike', 3);
$table->recordResult('Arnold', 5);
$table->recordResult('Chris', 5);
echo $table->playerRank(1);
@litto
Copy link

litto commented Feb 12, 2022

Slight Changes needed in code for it to pass all the conditions.. I will attach screenshots also..
Screenshot_767
Screenshot_766
change the code to :-
`<?php

class LeagueTable
{
public function __construct(array $players)
{
$this->standings = [];
foreach($players as $index => $p) {
$this->standings[$p] = [
'index' => $index,
'games_played' => 0,
'score' => 0,
'rank' => $index
];
}
}

public function recordResult(string $player, int $score) : void
{
    $this->standings[$player]['games_played']++;
    $this->standings[$player]['score'] += $score;
}

public function swapfunc(&$a,&$b)
{

    $temp=$b;
    $b=$a;
    $a=$temp;
    return true;

}

public function rerank(){

   foreach($this->standings as $player=>$attributes)
   {

       foreach($this->standings as $player1=>$attributes1)
       {

          if($attributes['score']>$attributes1['score'] && $attributes['rank']>$attributes1['rank'])
          {

            $this->swapfunc($this->standings[$player]['rank'],$this->standings[$player1]['rank']);
          }

          if($attributes['score']==$attributes1['score'] && $attributes['games_played']<$attributes1['games_played']&& $attributes['rank']>$attributes1['rank'])
          {

            $this->swapfunc($this->standings[$player]['rank'],$this->standings[$player1]['rank']);
          }


          if($attributes['score']==$attributes1['score'] && $attributes['games_played']==$attributes1['games_played'] && $attributes['index']<$attributes1['index'] && $attributes['rank']>$attributes1['rank'])
          {

            $this->swapfunc($this->standings[$player]['rank'],$this->standings[$player1]['rank']);
          }






       }

   }

return true;



}

public function playerRank(int $rank) : string
{
    $this->rerank();

   foreach ($this->standings as $key => $value) {

    if($this->standings[$key]['rank']==$rank-1)
        return $key;
   }
    
}

}

$table = new LeagueTable(array('Mike', 'Chris', 'Arnold'));
$table->recordResult('Mike', 2);
$table->recordResult('Mike', 3);
$table->recordResult('Arnold', 5);
$table->recordResult('Chris', 5);
echo $table->playerRank(1);`

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment