-
Notifications
You must be signed in to change notification settings - Fork 0
/
leagueTable.php
46 lines (40 loc) · 1.27 KB
/
leagueTable.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
<?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
];
}
}
public function recordResult(string $player, int $score) : void
{
$this->standings[$player]['games_played']++;
$this->standings[$player]['score'] += $score;
}
public function playerRank(int $rank) : string
{
$players = $this->standings;
uasort($players, function($a, $b){
if($a['score'] == $b['score'])
{
if($a['games_played'] == $b['games_played'])
return ($a['index'] < $b['index']) ? -1 : 1;
return ($a['games_played'] < $b['games_played']) ? -1 : 1;
}
return ($a['score'] > $b['score']) ? -1 : 1;
});
return array_keys($players)[$rank - 1];
}
}
$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);