<?php
class Klasemen
{
private $klubPoin = [];
public function __construct
(array $daftarKlub) {
foreach ($daftarKlub as $club) {
$this->klubPoin[$club] = 0;
}
}
public function resultPermainan(string $clubKandang, string $clubTandang, string $skor)
{
list($skorKandang, $skorTandang) = explode(':', $skor);
if ($skorKandang > $skorTandang) {
$this->klubPoin[$clubKandang] += 3;
} elseif ($skorKandang < $skorTandang) {
$this->klubPoin[$clubTandang] += 3;
} else {
$this->klubPoin[$clubKandang] += 1;
$this->klubPoin[$clubTandang] += 1;
}
}
public function baganKlasemen
(): array {
uksort($this->klubPoin, function($a, $b) { if ($this->klubPoin[$a] === $this->klubPoin[$b]) {
}
return $this->klubPoin[$b] - $this->klubPoin[$a];
});
return $this->klubPoin;
}
public function ambilPeringkat(int $nomorPeringkat): string
{
return $klub[$nomorPeringkat - 1] ?? 'Tidak ada';
}
}
// Contoh penggunaan
$klasemen = new Klasemen(['Indonesia', 'Korsel', 'Australia']);
$klasemen->resultPermainan('Indonesia', 'Korsel', '2:1');
$klasemen->resultPermainan('Indonesia', 'Australia', '2:1');
$klasemen->resultPermainan('Korsel', 'Indonesia', '2:2');
$klasemen->resultPermainan('Korsel', 'Australia', '3:2');
$klasemen->resultPermainan('Australia', 'Indonesia', '3:2');
$klasemen->resultPermainan('Australia', 'Korsel', '3:3');
print_r($klasemen->baganKlasemen()); echo $klasemen->ambilPeringkat(1);
?>
PD9waHAKCmNsYXNzIEtsYXNlbWVuCnsKICAgIHByaXZhdGUgJGtsdWJQb2luID0gW107CiAgICAKICAgIHB1YmxpYyBmdW5jdGlvbiBfX2NvbnN0cnVjdChhcnJheSAkZGFmdGFyS2x1YikKICAgIHsKICAgICAgICBmb3JlYWNoICgkZGFmdGFyS2x1YiBhcyAkY2x1YikgewogICAgICAgICAgICAkdGhpcy0+a2x1YlBvaW5bJGNsdWJdID0gMDsKICAgICAgICB9CiAgICB9CiAgICAKICAgIHB1YmxpYyBmdW5jdGlvbiByZXN1bHRQZXJtYWluYW4oc3RyaW5nICRjbHViS2FuZGFuZywgc3RyaW5nICRjbHViVGFuZGFuZywgc3RyaW5nICRza29yKQogICAgewogICAgICAgIGxpc3QoJHNrb3JLYW5kYW5nLCAkc2tvclRhbmRhbmcpID0gZXhwbG9kZSgnOicsICRza29yKTsKICAgICAgICAKICAgICAgICBpZiAoJHNrb3JLYW5kYW5nID4gJHNrb3JUYW5kYW5nKSB7CiAgICAgICAgICAgICR0aGlzLT5rbHViUG9pblskY2x1YkthbmRhbmddICs9IDM7CiAgICAgICAgfSBlbHNlaWYgKCRza29yS2FuZGFuZyA8ICRza29yVGFuZGFuZykgewogICAgICAgICAgICAkdGhpcy0+a2x1YlBvaW5bJGNsdWJUYW5kYW5nXSArPSAzOwogICAgICAgIH0gZWxzZSB7CiAgICAgICAgICAgICR0aGlzLT5rbHViUG9pblskY2x1YkthbmRhbmddICs9IDE7CiAgICAgICAgICAgICR0aGlzLT5rbHViUG9pblskY2x1YlRhbmRhbmddICs9IDE7CiAgICAgICAgfQogICAgfQogICAgCiAgICBwdWJsaWMgZnVuY3Rpb24gYmFnYW5LbGFzZW1lbigpOiBhcnJheQogICAgewogICAgICAgIHVrc29ydCgkdGhpcy0+a2x1YlBvaW4sIGZ1bmN0aW9uKCRhLCAkYikgewogICAgICAgICAgICBpZiAoJHRoaXMtPmtsdWJQb2luWyRhXSA9PT0gJHRoaXMtPmtsdWJQb2luWyRiXSkgewogICAgICAgICAgICAgICAgcmV0dXJuIHN0cmNtcCgkYSwgJGIpOwogICAgICAgICAgICB9CiAgICAgICAgICAgIHJldHVybiAkdGhpcy0+a2x1YlBvaW5bJGJdIC0gJHRoaXMtPmtsdWJQb2luWyRhXTsKICAgICAgICB9KTsKICAgICAgICByZXR1cm4gJHRoaXMtPmtsdWJQb2luOwogICAgfQogICAgCiAgICBwdWJsaWMgZnVuY3Rpb24gYW1iaWxQZXJpbmdrYXQoaW50ICRub21vclBlcmluZ2thdCk6IHN0cmluZwogICAgewogICAgICAgICRrbHViID0gYXJyYXlf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