From 87f4255282a97a473b07158da3162b9c3f9ea582 Mon Sep 17 00:00:00 2001 From: Luke Manyamazi Date: Mon, 20 Oct 2025 22:25:43 +0200 Subject: [PATCH 1/2] added prep exercises --- Prep Exercises/bank.py | 34 +++++++++++++++++++++++++++++ Prep Exercises/classes.py | 24 +++++++++++++++++++++ Prep Exercises/dataclass.py | 23 ++++++++++++++++++++ Prep Exercises/double22.py | 6 ++++++ Prep Exercises/generics.py | 20 +++++++++++++++++ Prep Exercises/laptops.py | 42 ++++++++++++++++++++++++++++++++++++ Prep Exercises/methods.py | 26 ++++++++++++++++++++++ Prep Exercises/typeLimits.py | 8 +++++++ test.py | 5 +++++ 9 files changed, 188 insertions(+) create mode 100644 Prep Exercises/bank.py create mode 100644 Prep Exercises/classes.py create mode 100644 Prep Exercises/dataclass.py create mode 100644 Prep Exercises/double22.py create mode 100644 Prep Exercises/generics.py create mode 100644 Prep Exercises/laptops.py create mode 100644 Prep Exercises/methods.py create mode 100644 Prep Exercises/typeLimits.py create mode 100644 test.py diff --git a/Prep Exercises/bank.py b/Prep Exercises/bank.py new file mode 100644 index 0000000..b8bfe8e --- /dev/null +++ b/Prep Exercises/bank.py @@ -0,0 +1,34 @@ +from typing import Dict + +# baances is a Dict mapping account names(str) to balances (int) +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 = total_pence // 100 + pence = total_pence % 100 + return f"£{pounds}.{pence:02d}" + + +balances: Dict[str, int] = { + "Sima": 700, + "Linn": 545, + "Georg": 831, +} + +open_account(balances, "Tobi", 913) +open_account(balances, "Olya", 713) + +total_pence: int = sum_balances(balances) +total_string: str = format_pence_as_string(total_pence) + +print(f"The bank accounts total {total_string}") diff --git a/Prep Exercises/classes.py b/Prep Exercises/classes.py new file mode 100644 index 0000000..b5421be --- /dev/null +++ b/Prep Exercises/classes.py @@ -0,0 +1,24 @@ +class Person: + def __init__(self, name: str, age: int, preferred_operating_system: str): + self.name = name + self.age = age + self.preferred_operating_system = preferred_operating_system + +def is_adult(person: Person) -> bool: + return person.age >= 18 + +def location(person: Person) -> str: + return person.location + +imran = Person("Imran", 22, "Ubuntu") +print(imran.name) +# print(imran.address) # Person has no address attribute +print(imran.age) +print(is_adult(imran)) +print(location(imran)) + +eliza = Person("Eliza", 34, "Arch Linux") +print(eliza.name) +# print(eliza.address) # Person has no address attribute +print(eliza.age) +print(is_adult(eliza)) \ No newline at end of file diff --git a/Prep Exercises/dataclass.py b/Prep Exercises/dataclass.py new file mode 100644 index 0000000..892c268 --- /dev/null +++ b/Prep Exercises/dataclass.py @@ -0,0 +1,23 @@ +from dataclasses import dataclass +from datetime import date + +@dataclass(frozen=True) +class Person: + name: str + date_of_birth: date + preferred_operating_system: str + + def is_adult(self) -> bool: + today = date.today() + age = today.year - self.date_of_birth.year - ( + (today.month, today.day) < (self.date_of_birth.month, self.date_of_birth.day) + ) + return age >= 18 + +imran = Person(name="Imran", date_of_birth=date(2003, 7, 14), preferred_operating_system="Ubuntu") +print(imran) +print(imran.is_adult()) + +# Equality check +imran2 = Person(name="Imran", date_of_birth=date(2003, 7, 14), preferred_operating_system="Ubuntu") +print(imran == imran2) \ No newline at end of file diff --git a/Prep Exercises/double22.py b/Prep Exercises/double22.py new file mode 100644 index 0000000..a883b04 --- /dev/null +++ b/Prep Exercises/double22.py @@ -0,0 +1,6 @@ +def double(value): + return value * 2 + +print(double("22")) + +# I somehow expected it to return this answer, because the string "22" doubled is "2222" \ No newline at end of file diff --git a/Prep Exercises/generics.py b/Prep Exercises/generics.py new file mode 100644 index 0000000..9d0c1d4 --- /dev/null +++ b/Prep Exercises/generics.py @@ -0,0 +1,20 @@ +from dataclasses import dataclass +from typing import List + +@dataclass(frozen=True) +class Person: + name: str + children: List["Person"] + age: int + +fatma = Person(name="Fatma", age=6, children=[]) +aisha = Person(name="Aisha", age=13, children=[]) + +imran = Person(name="Imran", age=34, children=[fatma, aisha]) + +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/laptops.py b/Prep Exercises/laptops.py new file mode 100644 index 0000000..1d098e4 --- /dev/null +++ b/Prep Exercises/laptops.py @@ -0,0 +1,42 @@ +from dataclasses import dataclass +from typing import List + +@dataclass(frozen=True) +class Person: + name: str + age: int + preferred_operating_systems: List[str] + + +@dataclass(frozen=True) +class Laptop: + id: int + manufacturer: str + model: str + screen_size_in_inches: float + operating_system: str + + +def find_possible_laptops(laptops: List[Laptop], person: Person) -> List[Laptop]: + possible_laptops = [] + for laptop in laptops: + if laptop.operating_system in person.preferred_operating_systems: + possible_laptops.append(laptop) + return possible_laptops + + +people = [ + Person(name="Imran", age=22, preferred_operating_systems=["Ubuntu"]), + Person(name="Eliza", age=34, preferred_operating_systems=["Arch Linux"]), +] + +laptops = [ + Laptop(id=1, manufacturer="Dell", model="XPS", screen_size_in_inches=13, operating_system="Arch Linux"), + Laptop(id=2, manufacturer="Dell", model="XPS", screen_size_in_inches=15, operating_system="Ubuntu"), + Laptop(id=3, manufacturer="Dell", model="XPS", screen_size_in_inches=15, operating_system="ubuntu"), + Laptop(id=4, manufacturer="Apple", model="macBook", screen_size_in_inches=13, operating_system="macOS"), +] + +for person in people: + possible_laptops = find_possible_laptops(laptops, person) + print(f"Possible laptops for {person.name}: {possible_laptops}") \ No newline at end of file diff --git a/Prep Exercises/methods.py b/Prep Exercises/methods.py new file mode 100644 index 0000000..ce234d2 --- /dev/null +++ b/Prep Exercises/methods.py @@ -0,0 +1,26 @@ +from datetime import date + +class Person: + def __init__(self, name: str, date_of_birth: date, preferred_operating_system: str): + self.name = name + self.date_of_birth = date_of_birth + self.preferred_operating_system = preferred_operating_system + + @property + def age(self) -> int: + today = date.today() + return today.year - self.date_of_birth.year - ( + (today.month, today.day) < (self.date_of_birth.month, self.date_of_birth.day) + ) + + def is_adult(self) -> bool: + return self.age >= 18 + +imran = Person( + name="Imran", + date_of_birth=date(2003, 7, 14), + preferred_operating_system="Ubuntu" +) + +print(f"{imran.name} is {imran.age} years old.") +print(f"Is {imran.name} an adult? {imran.is_adult()}") diff --git a/Prep Exercises/typeLimits.py b/Prep Exercises/typeLimits.py new file mode 100644 index 0000000..5ea3d73 --- /dev/null +++ b/Prep Exercises/typeLimits.py @@ -0,0 +1,8 @@ +def double(number): + return number * 3 + +print(double(10)) + +# I expected it to return 20, because I thought it would double the number +# but it actually triples it to return 30 +# Fix - change the multiplication factor from 3 to 2 \ No newline at end of file diff --git a/test.py b/test.py new file mode 100644 index 0000000..0643990 --- /dev/null +++ b/test.py @@ -0,0 +1,5 @@ +def double(n: int) -> int: + return n * 2 + +num = double(21) +print(num) \ No newline at end of file From 35368a175b924c877656cc1d64cb333a0524558f Mon Sep 17 00:00:00 2001 From: Luke-Manyamazi Date: Fri, 24 Oct 2025 06:46:00 +0200 Subject: [PATCH 2/2] fixed names --- Prep Exercises/bank.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/Prep Exercises/bank.py b/Prep Exercises/bank.py index b8bfe8e..b971995 100644 --- a/Prep Exercises/bank.py +++ b/Prep Exercises/bank.py @@ -1,6 +1,6 @@ from typing import Dict -# baances is a Dict mapping account names(str) to balances (int) +# balances is a Dict mapping account names(str) to balances (int) def open_account(balances: Dict[str, int], name: str, amount: int) -> None: balances[name] = amount @@ -22,11 +22,11 @@ def format_pence_as_string(total_pence: int) -> str: balances: Dict[str, int] = { "Sima": 700, "Linn": 545, - "Georg": 831, + "George": 831, } open_account(balances, "Tobi", 913) -open_account(balances, "Olya", 713) +open_account(balances, "Ola", 713) total_pence: int = sum_balances(balances) total_string: str = format_pence_as_string(total_pence)