From c5fee461f145bde9348bd4d12b2124f46f9a2a28 Mon Sep 17 00:00:00 2001 From: eyuell21 Date: Wed, 15 Oct 2025 13:34:26 +0100 Subject: [PATCH 1/8] First Prep Exercise --- 1.py | 23 +++++++++++++++++++++++ 1 file changed, 23 insertions(+) create mode 100644 1.py diff --git a/1.py b/1.py new file mode 100644 index 0000000..16379d2 --- /dev/null +++ b/1.py @@ -0,0 +1,23 @@ +def double(number): + return number * 3 + +print(double(10)) + + +""" +Here the name of the function is double but on line 3 it returns the triple of the a given number +""" + +#Ideal solutions: + +def triple(number): + return number * 3 + +print(double(10)) + +"or" + +def double(number): + return number * 2 + +print(double(10)) \ No newline at end of file From de7c63ac26cc208220166a996ed1cc30cd6ecc71 Mon Sep 17 00:00:00 2001 From: eyuell21 Date: Wed, 15 Oct 2025 13:34:44 +0100 Subject: [PATCH 2/8] 2nd Prep exercise --- 2.py | 42 ++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 42 insertions(+) create mode 100644 2.py diff --git a/2.py b/2.py new file mode 100644 index 0000000..8b21c0a --- /dev/null +++ b/2.py @@ -0,0 +1,42 @@ +from typing import Dict, Union + +def open_account(balances: Dict[str, int], name: str, amount: Union[int, float, str]) -> None: + if isinstance(amount, str) and amount.startswith("£"): + pounds, pence = map(int, amount[1:].split(".")) + amount = pounds * 100 + pence + elif isinstance(amount, float): + amount = int(round(amount * 100)) + elif isinstance(amount, int): + pass + else: + raise ValueError("Unsupported amount format") + balances[name] = amount + +def sum_balances(accounts: Dict[str, int]) -> int: + total: int = 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: int = total_pence % 100 + return f"£{pounds}.{pence:02d}" + +# Main block +balances: Dict[str, int] = { + "Sima": 700, + "Linn": 545, + "Georg": 831, +} + +open_account(balances, "Tobi", 9.13) # float input +open_account(balances, "Olya", "£7.13") # string input + +total_pence: int = sum_balances(balances) +total_string: str = format_pence_as_string(total_pence) + +print(f"The bank accounts total {total_string}") From b01be3c6c7a3c638ea866dd804bded8eb4c43dc3 Mon Sep 17 00:00:00 2001 From: eyuell21 Date: Wed, 15 Oct 2025 13:34:55 +0100 Subject: [PATCH 3/8] 3rd Prep exercise --- 3.py | 47 +++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 47 insertions(+) create mode 100644 3.py diff --git a/3.py b/3.py new file mode 100644 index 0000000..f0e0d53 --- /dev/null +++ b/3.py @@ -0,0 +1,47 @@ +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}") + +# Person class +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 + +imran = Person("Imran", 22, "Ubuntu") + +# Corrected is_adult function +def is_adult(person: Person) -> bool: + return person.age >= 18 + +print(is_adult(imran)) # Output: True From 0f7f76f84e14e1b00b7fed6a190a195e01786d78 Mon Sep 17 00:00:00 2001 From: eyuell21 Date: Wed, 15 Oct 2025 13:35:09 +0100 Subject: [PATCH 4/8] 4th Prep exercise --- 4.py | 53 +++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 53 insertions(+) create mode 100644 4.py diff --git a/4.py b/4.py new file mode 100644 index 0000000..0f4855a --- /dev/null +++ b/4.py @@ -0,0 +1,53 @@ +from typing import Dict +from datetime import date + +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}") + +# Updated Person class +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 + +# Updated is_adult function +def is_adult(person: Person) -> bool: + today = date.today() + dob = person.date_of_birth + age = today.year - dob.year - ((today.month, today.day) < (dob.month, dob.day)) + return age >= 18 + +# Example usage +imran_dob = date(2003, 9, 10) +imran = Person("Imran", imran_dob, "Ubuntu") + +print(is_adult(imran)) # Output: True From 4864fbe6081928b14d2dc2f5a7819a634686c78f Mon Sep 17 00:00:00 2001 From: eyuell21 Date: Wed, 15 Oct 2025 13:35:20 +0100 Subject: [PATCH 5/8] 5th Prep exercise --- 5.py | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) create mode 100644 5.py diff --git a/5.py b/5.py new file mode 100644 index 0000000..58395b8 --- /dev/null +++ b/5.py @@ -0,0 +1,19 @@ +from dataclasses import dataclass +from datetime import date + +@dataclass +class Person: + name: str + date_of_birth: date + preferred_operating_system: str + + def is_adult(self) -> bool: + today = date.today() + dob = self.date_of_birth + age = today.year - dob.year - ((today.month, today.day) < (dob.month, dob.day)) + return age >= 18 + + +imran = Person(name="Imran", date_of_birth=date(2003, 9, 10), preferred_operating_system="Ubuntu") + +print(imran.is_adult()) # Output: True From 851097873b3ecefc8dc3205c9d2529f5758c46ad Mon Sep 17 00:00:00 2001 From: eyuell21 Date: Wed, 15 Oct 2025 13:35:29 +0100 Subject: [PATCH 6/8] 6th --- 6.py | 29 +++++++++++++++++++++++++++++ 1 file changed, 29 insertions(+) create mode 100644 6.py diff --git a/6.py b/6.py new file mode 100644 index 0000000..c28d424 --- /dev/null +++ b/6.py @@ -0,0 +1,29 @@ +from dataclasses import dataclass +from datetime import date +from typing import List + +@dataclass(frozen=True) +class Person: + name: str + date_of_birth: date + children: List["Person"] + + @property + def age(self) -> int: + today = date.today() + dob = self.date_of_birth + return today.year - dob.year - ((today.month, today.day) < (dob.month, dob.day)) + +def print_family_tree(person: Person, indent: int = 0) -> None: + spacer = " " * indent + print(f"{spacer}{person.name} (Age: {person.age})") + for child in person.children: + print_family_tree(child, indent + 1) + +# Sample data +fatma = Person(name="Fatma", date_of_birth=date(2012, 5, 14), children=[]) +aisha = Person(name="Aisha", date_of_birth=date(2015, 8, 3), children=[]) +imran = Person(name="Imran", date_of_birth=date(1985, 2, 20), children=[fatma, aisha]) + +# Output family tree +print_family_tree(imran) From 982f22f486c77878291eb42ece1e839333ac69d6 Mon Sep 17 00:00:00 2001 From: eyuell21 Date: Wed, 15 Oct 2025 13:35:40 +0100 Subject: [PATCH 7/8] 7thPrep exercise --- 7.py | 42 ++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 42 insertions(+) create mode 100644 7.py diff --git a/7.py b/7.py new file mode 100644 index 0000000..e4015a7 --- /dev/null +++ b/7.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_systems: List[str] + + +def find_possible_laptops(laptops: List[Laptop], person: Person) -> List[Laptop]: + possible_laptops = [] + for laptop in laptops: + if laptop.operating_systems == 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_systems=["Arch Linux"]), + Laptop(id=2, manufacturer="Dell", model="XPS", screen_size_in_inches=15, operating_systems=["Ubuntu"]), + Laptop(id=3, manufacturer="Dell", model="XPS", screen_size_in_inches=15, operating_systems=["ubuntu"]), + Laptop(id=4, manufacturer="Apple", model="macBook", screen_size_in_inches=13, operating_systems=["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 From 27eb7d3144788da23c4205afd807a94c3f313e61 Mon Sep 17 00:00:00 2001 From: eyuell21 Date: Thu, 23 Oct 2025 23:10:43 +0100 Subject: [PATCH 8/8] Laptop allocation. --- laptop_allocation.py | 88 ++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 88 insertions(+) create mode 100644 laptop_allocation.py diff --git a/laptop_allocation.py b/laptop_allocation.py new file mode 100644 index 0000000..98c385c --- /dev/null +++ b/laptop_allocation.py @@ -0,0 +1,88 @@ +import sys +from dataclasses import dataclass +from enum import Enum +from typing import List +from collections import Counter + + +class OperatingSystem(Enum): + MACOS = "macOS" + ARCH = "Arch Linux" + UBUNTU = "Ubuntu" + + @classmethod + def from_string(cls, s: str): + for os in cls: + if os.value.lower() == s.lower(): + return os + raise ValueError(f"Unsupported operating system: {s}") + + +@dataclass(frozen=True) +class Person: + name: str + age: int + preferred_operating_system: OperatingSystem + + +@dataclass(frozen=True) +class Laptop: + id: int + manufacturer: str + model: str + screen_size_in_inches: float + operating_system: OperatingSystem + + +def find_possible_laptops(laptops: List[Laptop], person: Person) -> List[Laptop]: + return [laptop for laptop in laptops if laptop.operating_system == person.preferred_operating_system] + + +def main(): + # Existing inventory + laptops = [ + Laptop(id=1, manufacturer="Dell", model="XPS", screen_size_in_inches=13, operating_system=OperatingSystem.ARCH), + Laptop(id=2, manufacturer="Dell", model="XPS", screen_size_in_inches=15, operating_system=OperatingSystem.UBUNTU), + Laptop(id=3, manufacturer="Dell", model="XPS", screen_size_in_inches=15, operating_system=OperatingSystem.UBUNTU), + Laptop(id=4, manufacturer="Apple", model="macBook", screen_size_in_inches=13, operating_system=OperatingSystem.MACOS), + ] + + # Read and validate user input + try: + name = input("Enter your name: ").strip() + if not name: + raise ValueError("Name cannot be empty.") + + age_input = input("Enter your age: ").strip() + age = int(age_input) + if age <= 0: + raise ValueError("Age must be a positive integer.") + + print("Available operating systems:") + for os in OperatingSystem: + print(f"- {os.value}") + os_input = input("Enter your preferred operating system: ").strip() + preferred_os = OperatingSystem.from_string(os_input) + + except ValueError as e: + print(f"Error: {e}", file=sys.stderr) + sys.exit(1) + + # Create the person + person = Person(name=name, age=age, preferred_operating_system=preferred_os) + + # Check availability + possible_laptops = find_possible_laptops(laptops, person) + print(f"\nThere are {len(possible_laptops)} laptops available with {preferred_os.value}.") + + # Count OS distribution + os_counts = Counter(laptop.operating_system for laptop in laptops) + most_common_os, most_common_count = os_counts.most_common(1)[0] + + if most_common_os != preferred_os and most_common_count > len(possible_laptops): + print(f"Note: There are more laptops available with {most_common_os.value} ({most_common_count} total).") + print(f"If you're willing to accept {most_common_os.value}, you're more likely to get a laptop.") + + +if __name__ == "__main__": + main()