-
Notifications
You must be signed in to change notification settings - Fork 1
/
LeagueTable.php
46 lines (40 loc) · 1.43 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
{
private $standings = [];
public function __construct(array $players)
{
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
{
$sortedPlayers = array_keys($this->standings);
usort($sortedPlayers, function ($a, $b) {
if ($this->standings[$a]['score'] == $this->standings[$b]['score']) {
if ($this->standings[$a]['games_played'] == $this->standings[$b]['games_played']) {
return $this->standings[$a]['index'] - $this->standings[$b]['index'];
}
return $this->standings[$a]['games_played'] - $this->standings[$b]['games_played'];
}
return $this->standings[$b]['score'] - $this->standings[$a]['score'];
});
return $sortedPlayers[$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);