class Klasemen():
def __init__(self, daftar_klub: list[str]):
# init klasemen klub
self.daftar_klub = {klub: 0 for klub in daftar_klub}
def catatPermainan(self, klubKandang: str, klubTandang: str, skor: str):
extract_skor = skor.split(":")
# ganti tipe data
skorKandang = int(extract_skor[0])
skorTandang = int(extract_skor[1])
if(skorKandang > skorTandang):
# klub kandang menang
self.daftar_klub[klubKandang] += 3
elif(skorKandang < skorTandang):
# klub tandang menang
self.daftar_klub[klubTandang] += 3
else:
# seri
self.daftar_klub[klubKandang] += 1
self.daftar_klub[klubTandang] += 1
# urutkan klasemen berdasarkan poin klub secara descending
self.daftar_klub = dict(sorted(self.daftar_klub.items(), key=lambda x: x[1], reverse=True))
def cetakKlasemen(self):
return list(self.daftar_klub.items())
def ambilPeringkat(self, peringkat: int):
return list(self.daftar_klub.items())[peringkat-1][0]
klasemen = Klasemen(['Liverpool', 'Chelsea', 'Arsenal'])
klasemen.catatPermainan('Arsenal', 'Liverpool', '2:1')
klasemen.catatPermainan('Arsenal', 'Chelsea', '1:1')
klasemen.catatPermainan('Chelsea', 'Arsenal', '0:3')
klasemen.catatPermainan('Chelsea', 'Liverpool', '3:2')
klasemen.catatPermainan('Liverpool', 'Arsenal', '2:2')
klasemen.catatPermainan('Liverpool', 'Chelsea', '0:0')
print(klasemen.cetakKlasemen())
print(klasemen.ambilPeringkat(2))
Y2xhc3MgS2xhc2VtZW4oKToKICAgIGRlZiBfX2luaXRfXyhzZWxmLCBkYWZ0YXJfa2x1YjogbGlzdFtzdHJdKToKICAgICAgICAjIGluaXQga2xhc2VtZW4ga2x1YgogICAgICAgIHNlbGYuZGFmdGFyX2tsdWIgPSB7a2x1YjogMCBmb3Iga2x1YiBpbiBkYWZ0YXJfa2x1Yn0KCiAgICBkZWYgY2F0YXRQZXJtYWluYW4oc2VsZiwga2x1YkthbmRhbmc6IHN0ciwga2x1YlRhbmRhbmc6IHN0ciwgc2tvcjogc3RyKToKICAgICAgICBleHRyYWN0X3Nrb3IgPSBza29yLnNwb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