diff --git a/prep exercises/__pycache__/dataclasses.cpython-312.pyc b/prep exercises/__pycache__/dataclasses.cpython-312.pyc new file mode 100644 index 0000000..9efc78f Binary files /dev/null and b/prep exercises/__pycache__/dataclasses.cpython-312.pyc differ 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/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()) 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 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