Skip to content

Commit fe955b3

Browse files
committed
I made the ages to be dynamically calculated
1 parent 9b3ad1e commit fe955b3

1 file changed

Lines changed: 17 additions & 6 deletions

File tree

prep exercises/generic_exercise.py

Lines changed: 17 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,33 @@
11
from dataclasses import dataclass
22
from typing import List
3+
from datetime import date
34

45
@dataclass(frozen=True)
56

67
class Person:
78
name: str
89
children: List["Person"]
9-
age: int
10+
date_of_birth: str
1011

11-
fatma = Person(name="Fatma", children=[], age=25)
12-
aisha = Person(name="Aisha", children=[], age = 15)
12+
def person_age(self) -> int:
13+
today_date = date.today()
14+
birth_date = date.fromisoformat(self.date_of_birth)
15+
age = today_date.year - birth_date.year
1316

14-
imran = Person(name="Imran", children=[fatma, aisha], age = 40)
17+
if (today_date.month, today_date.day) < (birth_date.month, birth_date.day):
18+
age -= 1
19+
return age
20+
21+
22+
fatma = Person(name="Fatma", children=[], date_of_birth="2010-06-15")
23+
aisha = Person(name="Aisha", children=[], date_of_birth="2012-09-20")
24+
25+
imran = Person(name="Imran", children=[fatma, aisha], date_of_birth="1998-10-16")
1526

1627
def print_family_tree(person: Person) -> None:
17-
print(person.name)
28+
print(f"{person.name} (Age: {person.person_age()})")
1829

1930
for child in person.children:
20-
print(f"- {child.name} ({child.age})")
31+
print(f"- {child.name} ({child.person_age()})")
2132

2233
print_family_tree(imran)

0 commit comments

Comments
 (0)