-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
49 lines (32 loc) · 1.55 KB
/
main.py
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
47
48
49
import json
from pydantic import BaseModel
class Winner(BaseModel):
country: str
year: int
competition: str
def get_world_cup_data() -> list[Winner]:
data = json.load(open("worldcupdata.json", "r"))
winners = [Winner(**w) for w in data]
return winners
def get_womens_winners_by_country(country_name: str) -> list[Winner]:
# filter by country
# f = lambda w: w.country == country_name and w.competition == "women"
return list(filter(lambda w: w.country == country_name and w.competition == "women", get_world_cup_data()))
def get_mens_winners_by_country(country_name: str) -> list[Winner]:
return [winner for winner in get_world_cup_data() if winner.country == country_name and winner.competition == "men"]
def get_winners_by_country(country_name: str) -> list[Winner]:
return [winner for winner in get_world_cup_data() if winner.country == country_name]
def get_mens_winners_all() -> list[Winner]:
return list(filter(lambda w: w.competition == "men", get_world_cup_data()))
def print_world_cup_data(data: list[Winner]):
# sort by year before printing
data.sort(key=lambda w: w.year)
for winner in data:
print(f"{winner.year} - {winner.country} ({winner.competition})")
if __name__ == '__main__':
data = get_world_cup_data()
# print_world_cup_data(data)
# print_world_cup_data(get_womens_winners_by_country("Spain"))
# print_world_cup_data(get_mens_winners_by_country("Spain"))
# print_world_cup_data(get_winners_by_country("Spain"))
print_world_cup_data(get_mens_winners_all())