diff --git a/prep exercises/bank_account.py b/prep exercises/bank_account.py new file mode 100644 index 0000000..2169ed3 --- /dev/null +++ b/prep exercises/bank_account.py @@ -0,0 +1,32 @@ +from typing import Dict + +def open_account(balances: Dict[str, int], name: str, amount: int) -> None: + balances[name] = amount + +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: int) -> str: + 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", 913) +open_account(balances, "Olya", 713) + +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/dataclasses_exercise.py b/prep exercises/dataclasses_exercise.py new file mode 100644 index 0000000..e4b8c40 --- /dev/null +++ b/prep exercises/dataclasses_exercise.py @@ -0,0 +1,23 @@ +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 + + if (today_date.month, today_date.day) < (birth_date.month, birth_date.day): + age -= 1 + + return age >= 18 + + +imran = Person("Imran", "2007-11-02", "Ubuntu") + +print(imran.is_adult()) diff --git a/prep exercises/generic_exercise.py b/prep exercises/generic_exercise.py new file mode 100644 index 0000000..6692dcb --- /dev/null +++ b/prep exercises/generic_exercise.py @@ -0,0 +1,33 @@ +from dataclasses import dataclass +from typing import List +from datetime import date + +@dataclass(frozen=True) + +class Person: + name: str + children: List["Person"] + date_of_birth: str + + def person_age(self) -> int: + today_date = date.today() + 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 + + +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(f"{person.name} (Age: {person.person_age()})") + + for child in person.children: + print(f"- {child.name} ({child.person_age()})") + +print_family_tree(imran) \ 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..7745258 --- /dev/null +++ b/prep exercises/test.py @@ -0,0 +1,5 @@ +def triple(n): + return n * 3 + +num = triple(21) +print(num) \ No newline at end of file