class Klasemen:
def __init__(self, klub_list):
# Inisialisasi klub dengan poin awal 0
self.poin = {klub: 0 for klub in klub_list}
def catat_permainan(self, klub_kandang, klub_tandang, skor):
# Memisahkan skor menjadi gol kandang dan gol tandang
gol_kandang, gol_tandang = map(int, skor.split(':'))
if gol_kandang > gol_tandang:
# Klub kandang menang
self.poin[klub_kandang] += 3
elif gol_kandang < gol_tandang:
# Klub tandang menang
self.poin[klub_tandang] += 3
else:
# Seri
self.poin[klub_kandang] += 1
self.poin[klub_tandang] += 1
def cetak_klasemen(self):
# Urutkan klub berdasarkan poin (desc) dan alfabet jika poin sama
return dict(sorted(self.poin.items(), key=lambda x: (-x[1], x[0])))
def ambil_peringkat(self, nomor_peringkat):
# Ambil daftar klub berdasarkan peringkat
klasemen = self.cetak_klasemen()
klub_list = list(klasemen.keys())
# Kembalikan nama klub pada peringkat yang diminta (1-based index)
return klub_list[nomor_peringkat - 1] if 0 < nomor_peringkat <= len(klub_list) else None
# Contoh penggunaan
klasemen = Klasemen(['Liverpool', 'Chelsea', 'Arsenal'])
klasemen.catat_permainan('Arsenal', 'Liverpool', '2:1')
klasemen.catat_permainan('Arsenal', 'Chelsea', '1:1')
klasemen.catat_permainan('Chelsea', 'Arsenal', '0:3')
klasemen.catat_permainan('Chelsea', 'Liverpool', '3:2')
klasemen.catat_permainan('Liverpool', 'Arsenal', '2:2')
klasemen.catat_permainan('Liverpool', 'Chelsea', '0:0')
# Cetak klasemen
print(klasemen.cetak_klasemen()) # Output: {'Arsenal': 8, 'Chelsea': 5, 'Liverpool': 2}
# Ambil peringkat ke-2
print(klasemen.ambil_peringkat(2)) # Output: 'Chelsea'
Y2xhc3MgS2xhc2VtZW46CiAgICBkZWYgX19pbml0X18oc2VsZiwga2x1Yl9saXN0KToKICAgICAgICAjIEluaXNpYWxpc2FzaSBrbHViIGRlbmdhbiBwb2luIGF3YWwgMAogICAgICAgIHNlbGYucG9pbiA9IHtrbHViOiAwIGZvciBrbHViIGluIGtsdWJfbGlz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