Skip to content

Commit 0de79e5

Browse files
authored
Merge pull request mouredev#3990 from hozlucas28/Solution-22-Python
#22 - Python
2 parents 3e2dc9a + b0d6d74 commit 0de79e5

File tree

1 file changed

+158
-0
lines changed

1 file changed

+158
-0
lines changed
Lines changed: 158 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,158 @@
1+
# pylint: disable=pointless-string-statement,missing-function-docstring,missing-module-docstring
2+
3+
from typing import TypeVar, Callable, TypedDict
4+
from datetime import date
5+
6+
7+
"""
8+
Higher order functions (HOF)...
9+
"""
10+
11+
print("Higher order functions (HOF)...")
12+
13+
T = TypeVar("T")
14+
15+
16+
def to_filtered(*, array: list[T], func: Callable[[T], bool]) -> list[T]:
17+
sorted_array: list[T] = []
18+
19+
for element in array:
20+
if func(element):
21+
sorted_array.append(element)
22+
23+
return sorted_array
24+
25+
26+
numbers: list[int] = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
27+
print(f"\n{numbers=}")
28+
29+
even_numbers: list[int] = to_filtered(
30+
array=numbers, func=lambda number: number % 2 == 0
31+
)
32+
print(f"\n{even_numbers=}")
33+
34+
odd_numbers: list[int] = to_filtered(array=numbers, func=lambda number: number % 2 != 0)
35+
print(f"\n{odd_numbers=}")
36+
37+
print(
38+
"\n# ---------------------------------------------------------------------------------- #\n"
39+
)
40+
41+
42+
"""
43+
Additional challenge...
44+
"""
45+
46+
print("Additional challenge...\n")
47+
48+
Student = TypedDict(
49+
"Student",
50+
{
51+
"born_date": date,
52+
"name": str,
53+
"qualifications": list[float],
54+
},
55+
)
56+
57+
type K = int | float
58+
59+
60+
def get_average(*_numbers: K) -> K:
61+
total: K = _numbers[0]
62+
for number in _numbers[1:]:
63+
total += number
64+
65+
return total / len(_numbers)
66+
67+
68+
students: list[Student] = [
69+
{
70+
"born_date": date(year=2000, month=1, day=1),
71+
"name": "John Doe",
72+
"qualifications": [8.5, 9.0, 7.5],
73+
},
74+
{
75+
"born_date": date(year=1999, month=5, day=10),
76+
"name": "Jane Smith",
77+
"qualifications": [9.5, 9.5, 9.0],
78+
},
79+
{
80+
"born_date": date(year=2001, month=3, day=15),
81+
"name": "Michael Johnson",
82+
"qualifications": [7.0, 8.0, 6.5],
83+
},
84+
{
85+
"born_date": date(year=2002, month=7, day=20),
86+
"name": "Emily Davis",
87+
"qualifications": [9.0, 9.5, 8.5],
88+
},
89+
{
90+
"born_date": date(year=2000, month=9, day=5),
91+
"name": "David Wilson",
92+
"qualifications": [8.0, 7.5, 8.5],
93+
},
94+
{
95+
"born_date": date(year=1999, month=12, day=25),
96+
"name": "Olivia Martinez",
97+
"qualifications": [9.5, 9.0, 9.5],
98+
},
99+
{
100+
"born_date": date(year=2001, month=8, day=12),
101+
"name": "Sophia Anderson",
102+
"qualifications": [7.5, 8.5, 7.0],
103+
},
104+
{
105+
"born_date": date(year=2002, month=4, day=30),
106+
"name": "Daniel Thompson",
107+
"qualifications": [9.0, 8.0, 9.0],
108+
},
109+
]
110+
111+
print("Students...\n")
112+
for student in students:
113+
print(f"{student['name']} / {student['born_date']} / {student['qualifications']}")
114+
115+
students_with_name_and_average_qualification = list(
116+
map(
117+
lambda student: {
118+
"name": student["name"],
119+
"average_qualification": get_average(*student["qualifications"]),
120+
},
121+
students,
122+
)
123+
)
124+
125+
print("\nStudents with name and average qualification...\n")
126+
for student in students_with_name_and_average_qualification:
127+
print(f"{student['name']} / {student['average_qualification']:.2f}")
128+
129+
students_with_average_qualification_more_than_nine: list[Student] = to_filtered(
130+
array=students, func=lambda student: get_average(*student["qualifications"]) >= 9
131+
)
132+
133+
print("\nStudents with an average qualification more than nine...\n")
134+
for student in students_with_average_qualification_more_than_nine:
135+
print(f"{student['name']} / {student['born_date']} / {student['qualifications']}")
136+
137+
138+
sorted_students_by_born_date: list[Student] = sorted(
139+
students, key=lambda student: student["born_date"], reverse=True
140+
)
141+
142+
print("\nSorted students by born date (youngest to oldest)...\n")
143+
for student in sorted_students_by_born_date:
144+
print(f"{student['name']} / {student['born_date']} / {student['qualifications']}")
145+
146+
students_with_name_and_best_qualification = list(
147+
map(
148+
lambda student: {
149+
"name": student["name"],
150+
"best_qualification": max(*student["qualifications"]),
151+
},
152+
students,
153+
)
154+
)
155+
156+
print("\nStudents with name and best qualification...\n")
157+
for student in students_with_name_and_best_qualification:
158+
print(f"{student['name']} / {student['best_qualification']}")

0 commit comments

Comments
 (0)