From d7b5b7a2fcec0cef48fc60929e0309793da4c740 Mon Sep 17 00:00:00 2001 From: Gabriel Date: Fri, 17 Oct 2025 21:42:32 +0100 Subject: [PATCH 1/6] I just used datetime in a class to calculate the age of person class instances --- prep exercises/bank_account.py | 30 +++++++++++++++++++++++++++ prep exercises/objects_and_classes.py | 18 ++++++++++++++++ prep exercises/test.py | 5 +++++ 3 files changed, 53 insertions(+) create mode 100644 prep exercises/bank_account.py create mode 100644 prep exercises/objects_and_classes.py create mode 100644 prep exercises/test.py diff --git a/prep exercises/bank_account.py b/prep exercises/bank_account.py new file mode 100644 index 0000000..276c13f --- /dev/null +++ b/prep exercises/bank_account.py @@ -0,0 +1,30 @@ +def open_account(balances, name, amount): + balances[name] = amount + +def sum_balances(accounts): + total = 0 + for name, pence in accounts.items(): + print(f"{name} had balance {pence}") + total += pence + return total + +def format_pence_as_string(total_pence): + if total_pence < 100: + return f"{total_pence}p" + pounds = int(total_pence / 100) + pence = total_pence % 100 + return f"£{pounds}.{pence:02d}" + +balances = { + "Sima": 700, + "Linn": 545, + "Georg": 831, +} + +open_account(balances, "Tobi", 9.13) +open_account(balances, "Olya", "£7.13") + +total_pence = sum_balances(balances) +total_string = format_pence_as_string(total_pence) + +print(f"The bank accounts total {total_string}") \ No newline at end of file diff --git a/prep exercises/objects_and_classes.py b/prep exercises/objects_and_classes.py new file mode 100644 index 0000000..a233a7f --- /dev/null +++ b/prep exercises/objects_and_classes.py @@ -0,0 +1,18 @@ +from datetime import date + +class Person: + def __init__(self, name: str, date_of_birth: str, preferred_operating_system: str, address: str): + self.name = name + self.date_of_birth = date.fromisoformat(date_of_birth) + self.preferred_operating_system = preferred_operating_system + self.address = address + + def is_adult(self): + today_date = date.today() + + return (today_date.year - self.date_of_birth.year) >= 18 + + +imran = Person("Imran", "2005-12-04", "Ubunut", "Wardend Road, Birmingham") + +print(imran.is_adult()) \ No newline at end of file diff --git a/prep exercises/test.py b/prep exercises/test.py new file mode 100644 index 0000000..098f359 --- /dev/null +++ b/prep exercises/test.py @@ -0,0 +1,5 @@ +def double(n): + return n * 3 + +num = double(21) +print(num) \ No newline at end of file From c172120e45593801535a70e8b061ec43e8997316 Mon Sep 17 00:00:00 2001 From: Gabriel Date: Fri, 17 Oct 2025 22:10:13 +0100 Subject: [PATCH 2/6] I used dataclass to help shortened my code and define the type of the object --- .../__pycache__/dataclasses.cpython-312.pyc | Bin 0 -> 751 bytes prep exercises/dataclasses_exercise.py | 20 ++++++++++++++++++ 2 files changed, 20 insertions(+) create mode 100644 prep exercises/__pycache__/dataclasses.cpython-312.pyc create mode 100644 prep exercises/dataclasses_exercise.py diff --git a/prep exercises/__pycache__/dataclasses.cpython-312.pyc b/prep exercises/__pycache__/dataclasses.cpython-312.pyc new file mode 100644 index 0000000000000000000000000000000000000000..9efc78fea2ff44dc3771216e5f3e637def32f8b8 GIT binary patch literal 751 zcmZ8fL2DC182x5VoIu#enTW5i}sVEZfYNHgtDqo!PX;Q=}D& zr{0>sAkkmqu@%aIhv2EVXbwU_aAuPfaSq?S@6DU{W`_B?uuud&Zh!f;{YTF~OfwsF z3dSEbI0gocL=ck(Az%R#W_`8?WTLU)DKO^%7#&y>W-#5{u(Z};Cz|}!uY;!j{%`U z7&NRppp6bAYnnx>k(DFE*#>o;Dpl?a6p|;(Nx~SF9d4moP6f752xI|IQG{7L*$QM= zW*9%Ox~dQaMjHgG7z8nAoyg!)5WMMx(R9U@nNZ~*2$O_o+JGlgYwUKCeS)z$^9A#_ zd5r#6xGCBg`8AX~8BhI}yp`>R0xLD##fYadCRybP?+A(hvpnlT4VyepxojI-U!OIt z;9C^UwnXV)z7@)cX;-*9Dlg`Vz{rL9EBhN~UTJ^*tg>?ItcYtfnW7u92$SxBvhE literal 0 HcmV?d00001 diff --git a/prep exercises/dataclasses_exercise.py b/prep exercises/dataclasses_exercise.py new file mode 100644 index 0000000..f48d26c --- /dev/null +++ b/prep exercises/dataclasses_exercise.py @@ -0,0 +1,20 @@ +from dataclasses import dataclass +from datetime import date + +@dataclass(frozen=True) +class Person: + name: str + date_of_birth: str + preferred_operating_system: str + + def is_adult(self): + today_date = date.today() + birth_date = date.fromisoformat(self.date_of_birth) + age = today_date.year - birth_date.year + + return age >= 18 + + +imran = Person("Imran", "2019-10-18", "Ubuntu") + +print(imran.is_adult()) From 8e2f3ce1e1de25f558d0bb21f3ff1fc9244eb2c9 Mon Sep 17 00:00:00 2001 From: Gabriel Date: Fri, 17 Oct 2025 22:42:24 +0100 Subject: [PATCH 3/6] Used list imported from typing to specify the type of the items in the list --- prep exercises/generic_exercise.py | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) create mode 100644 prep exercises/generic_exercise.py diff --git a/prep exercises/generic_exercise.py b/prep exercises/generic_exercise.py new file mode 100644 index 0000000..1429252 --- /dev/null +++ b/prep exercises/generic_exercise.py @@ -0,0 +1,22 @@ +from dataclasses import dataclass +from typing import List + +@dataclass(frozen=True) + +class Person: + name: str + children: List["Person"] + age: int + +fatma = Person(name="Fatma", children=[], age=25) +aisha = Person(name="Aisha", children=[], age = 15) + +imran = Person(name="Imran", children=[fatma, aisha], age = 40) + +def print_family_tree(person: Person) -> None: + print(person.name) + + for child in person.children: + print(f"- {child.name} ({child.age})") + +print_family_tree(imran) \ No newline at end of file From 63968ad132f5886979797645ff7392495020f36a Mon Sep 17 00:00:00 2001 From: Gabriel Date: Sat, 1 Nov 2025 16:12:45 +0000 Subject: [PATCH 4/6] I have added type annotation on the function --- .../__pycache__/dataclasses.cpython-312.pyc | Bin 751 -> 0 bytes prep exercises/bank_account.py | 12 +++++++----- prep exercises/test.py | 4 ++-- 3 files changed, 9 insertions(+), 7 deletions(-) delete mode 100644 prep exercises/__pycache__/dataclasses.cpython-312.pyc diff --git a/prep exercises/__pycache__/dataclasses.cpython-312.pyc b/prep exercises/__pycache__/dataclasses.cpython-312.pyc deleted file mode 100644 index 9efc78fea2ff44dc3771216e5f3e637def32f8b8..0000000000000000000000000000000000000000 GIT binary patch literal 0 HcmV?d00001 literal 751 zcmZ8fL2DC182x5VoIu#enTW5i}sVEZfYNHgtDqo!PX;Q=}D& zr{0>sAkkmqu@%aIhv2EVXbwU_aAuPfaSq?S@6DU{W`_B?uuud&Zh!f;{YTF~OfwsF z3dSEbI0gocL=ck(Az%R#W_`8?WTLU)DKO^%7#&y>W-#5{u(Z};Cz|}!uY;!j{%`U z7&NRppp6bAYnnx>k(DFE*#>o;Dpl?a6p|;(Nx~SF9d4moP6f752xI|IQG{7L*$QM= zW*9%Ox~dQaMjHgG7z8nAoyg!)5WMMx(R9U@nNZ~*2$O_o+JGlgYwUKCeS)z$^9A#_ zd5r#6xGCBg`8AX~8BhI}yp`>R0xLD##fYadCRybP?+A(hvpnlT4VyepxojI-U!OIt z;9C^UwnXV)z7@)cX;-*9Dlg`Vz{rL9EBhN~UTJ^*tg>?ItcYtfnW7u92$SxBvhE diff --git a/prep exercises/bank_account.py b/prep exercises/bank_account.py index 276c13f..2169ed3 100644 --- a/prep exercises/bank_account.py +++ b/prep exercises/bank_account.py @@ -1,14 +1,16 @@ -def open_account(balances, name, amount): +from typing import Dict + +def open_account(balances: Dict[str, int], name: str, amount: int) -> None: balances[name] = amount -def sum_balances(accounts): +def sum_balances(accounts: Dict[str, int]) -> int: total = 0 for name, pence in accounts.items(): print(f"{name} had balance {pence}") total += pence return total -def format_pence_as_string(total_pence): +def format_pence_as_string(total_pence: int) -> str: if total_pence < 100: return f"{total_pence}p" pounds = int(total_pence / 100) @@ -21,8 +23,8 @@ def format_pence_as_string(total_pence): "Georg": 831, } -open_account(balances, "Tobi", 9.13) -open_account(balances, "Olya", "£7.13") +open_account(balances, "Tobi", 913) +open_account(balances, "Olya", 713) total_pence = sum_balances(balances) total_string = format_pence_as_string(total_pence) diff --git a/prep exercises/test.py b/prep exercises/test.py index 098f359..7745258 100644 --- a/prep exercises/test.py +++ b/prep exercises/test.py @@ -1,5 +1,5 @@ -def double(n): +def triple(n): return n * 3 -num = double(21) +num = triple(21) print(num) \ No newline at end of file From 9b3ad1e4d0a8e9a61632aabf454d640e44b21702 Mon Sep 17 00:00:00 2001 From: Gabriel Date: Sat, 1 Nov 2025 17:44:31 +0000 Subject: [PATCH 5/6] These are the changes i made in method is_adult() --- prep exercises/dataclasses_exercise.py | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/prep exercises/dataclasses_exercise.py b/prep exercises/dataclasses_exercise.py index f48d26c..e4b8c40 100644 --- a/prep exercises/dataclasses_exercise.py +++ b/prep exercises/dataclasses_exercise.py @@ -12,9 +12,12 @@ def is_adult(self): birth_date = date.fromisoformat(self.date_of_birth) age = today_date.year - birth_date.year + if (today_date.month, today_date.day) < (birth_date.month, birth_date.day): + age -= 1 + return age >= 18 -imran = Person("Imran", "2019-10-18", "Ubuntu") +imran = Person("Imran", "2007-11-02", "Ubuntu") print(imran.is_adult()) From fe955b398b928c10d1df8bd0fc9b64f181f5a1bf Mon Sep 17 00:00:00 2001 From: Gabriel Date: Sat, 1 Nov 2025 23:52:37 +0000 Subject: [PATCH 6/6] I made the ages to be dynamically calculated --- prep exercises/generic_exercise.py | 23 +++++++++++++++++------ 1 file changed, 17 insertions(+), 6 deletions(-) diff --git a/prep exercises/generic_exercise.py b/prep exercises/generic_exercise.py index 1429252..6692dcb 100644 --- a/prep exercises/generic_exercise.py +++ b/prep exercises/generic_exercise.py @@ -1,22 +1,33 @@ from dataclasses import dataclass from typing import List +from datetime import date @dataclass(frozen=True) class Person: name: str children: List["Person"] - age: int + date_of_birth: str -fatma = Person(name="Fatma", children=[], age=25) -aisha = Person(name="Aisha", children=[], age = 15) + def person_age(self) -> int: + today_date = date.today() + birth_date = date.fromisoformat(self.date_of_birth) + age = today_date.year - birth_date.year -imran = Person(name="Imran", children=[fatma, aisha], age = 40) + if (today_date.month, today_date.day) < (birth_date.month, birth_date.day): + age -= 1 + return age + + +fatma = Person(name="Fatma", children=[], date_of_birth="2010-06-15") +aisha = Person(name="Aisha", children=[], date_of_birth="2012-09-20") + +imran = Person(name="Imran", children=[fatma, aisha], date_of_birth="1998-10-16") def print_family_tree(person: Person) -> None: - print(person.name) + print(f"{person.name} (Age: {person.person_age()})") for child in person.children: - print(f"- {child.name} ({child.age})") + print(f"- {child.name} ({child.person_age()})") print_family_tree(imran) \ No newline at end of file