From 7f59b07376ecd8618036e9667511bee1ee7e28d3 Mon Sep 17 00:00:00 2001 From: Fatma Degirmenci Date: Mon, 12 Jan 2026 20:43:04 +0000 Subject: [PATCH 1/8] Implement Enums exercise --- sprint5/enums.py | 61 ++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 61 insertions(+) create mode 100644 sprint5/enums.py diff --git a/sprint5/enums.py b/sprint5/enums.py new file mode 100644 index 00000000..f6cf935a --- /dev/null +++ b/sprint5/enums.py @@ -0,0 +1,61 @@ +from dataclasses import dataclass +from enum import Enum +from typing import List +import sys + +class OperatingSystem(Enum): + MACOS = "macOS" + ARCH = "Arch Linux" + UBUNTU = "Ubuntu" + +@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 + +laptops = [ + Laptop(1, "Dell", "XPS", 13, OperatingSystem.ARCH), + Laptop(2, "Dell", "XPS", 15, OperatingSystem.UBUNTU), + Laptop(3, "Dell", "XPS", 15, OperatingSystem.UBUNTU), + Laptop(4, "Apple", "macBook", 13, OperatingSystem.MACOS), +] + + +name = input("Name: ").strip() +age_input = input("Age: ").strip() +os_input = input("Preferred OS (macOS, Arch Linux, Ubuntu): ").strip() + +try: + age = int(age_input) +except ValueError: + print("Error: Age must be a number.", file=sys.stderr) + sys.exit(1) + +try: + preferred_os = OperatingSystem(os_input) +except ValueError: + print(f"Error: '{os_input}' is not a valid operating system.", file=sys.stderr) + sys.exit(1) + +person = Person(name=name, age=age, preferred_operating_system=preferred_os) + +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] + +possible_laptops = find_possible_laptops(laptops, person) +print(f"There are {len(possible_laptops)} laptops matching your preferred OS ({person.preferred_operating_system.value}).") + +other_counts = {os: sum(1 for l in laptops if l.operating_system == os) for os in OperatingSystem} +most_available_os = max(other_counts, key=other_counts.get) + +if most_available_os != person.preferred_operating_system and other_counts[most_available_os] > len(possible_laptops): + print(f"If you are willing to accept {most_available_os.value}, there are more laptops available ({other_counts[most_available_os]}).") From fd1094c8f6f2d41e3a9e740323163fd896c2f6c4 Mon Sep 17 00:00:00 2001 From: Fatma Degirmenci Date: Mon, 12 Jan 2026 21:46:17 +0000 Subject: [PATCH 2/8] Add type annotations --- sprint5/exercise1.py | 33 +++++++++++++++++++++++++++++++++ 1 file changed, 33 insertions(+) create mode 100644 sprint5/exercise1.py diff --git a/sprint5/exercise1.py b/sprint5/exercise1.py new file mode 100644 index 00000000..c200467b --- /dev/null +++ b/sprint5/exercise1.py @@ -0,0 +1,33 @@ +def open_account(balances:dict , name:str, amount:int): + 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:int): + 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", int(9.13*100)) +open_account(balances,"Olya", int(7.13*100)) + +total_pence = sum_balances(balances) +total_string = format_pence_as_string(total_pence) + +print(f"The bank accounts total {total_string}") + + + From 864e986df26bf0cf4cc7409f5a79cb872d8baf7f Mon Sep 17 00:00:00 2001 From: Fatma Degirmenci Date: Mon, 12 Jan 2026 21:47:25 +0000 Subject: [PATCH 3/8] Create Person class --- sprint5/exercise2.py | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) create mode 100644 sprint5/exercise2.py diff --git a/sprint5/exercise2.py b/sprint5/exercise2.py new file mode 100644 index 00000000..5cca3716 --- /dev/null +++ b/sprint5/exercise2.py @@ -0,0 +1,22 @@ +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") +print(imran.name) +print(imran.address) + +eliza = Person("Eliza", 34, "Arch Linux") +print(eliza.name) +print(eliza.address) + + +def is_adult(person: Person) -> bool: + return person.age >= 18 + +print(is_adult(imran)) + +def print_address(person: Person) -> str: + return person.country \ No newline at end of file From 79d0a12e94844410484ee334a57c1315b794df29 Mon Sep 17 00:00:00 2001 From: Fatma Degirmenci Date: Mon, 12 Jan 2026 21:48:18 +0000 Subject: [PATCH 4/8] Update the is_adult method --- sprint5/methods.py | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) create mode 100644 sprint5/methods.py diff --git a/sprint5/methods.py b/sprint5/methods.py new file mode 100644 index 00000000..d4761210 --- /dev/null +++ b/sprint5/methods.py @@ -0,0 +1,19 @@ +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 + + def is_adult(self) -> bool: + today=date.today() + age=today.year -self.date_of_birth.year + + if(today.month,today.day)<(self.date_of_birth.month,self.date_of_birth.day): + age-=1 + return age>=18 + +imran = Person("Imran", date(2001,9,16), "Ubuntu") +print(imran.is_adult()) \ No newline at end of file From 2e77268606f8fa7ad036acc332446b974ab64321 Mon Sep 17 00:00:00 2001 From: Fatma Degirmenci Date: Mon, 12 Jan 2026 21:49:14 +0000 Subject: [PATCH 5/8] Refactor Person class --- sprint5/data_classes.py | 24 ++++++++++++++++++++++++ 1 file changed, 24 insertions(+) create mode 100644 sprint5/data_classes.py diff --git a/sprint5/data_classes.py b/sprint5/data_classes.py new file mode 100644 index 00000000..4effa7af --- /dev/null +++ b/sprint5/data_classes.py @@ -0,0 +1,24 @@ +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 + + if(today.month,today.day)<(self.date_of_birth.month,self.date_of_birth.day): + age-=1 + return age>=18 + + + +imran = Person("Imran", date(2001, 9, 16), "Ubuntu") +print(imran) +print(imran.is_adult()) + \ No newline at end of file From 4db0fa1be72b43e074a7e6422341b7ed4857a4fb Mon Sep 17 00:00:00 2001 From: Fatma Degirmenci Date: Mon, 12 Jan 2026 21:51:22 +0000 Subject: [PATCH 6/8] Refactor Person class to fix code --- sprint5/generics.py | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) create mode 100644 sprint5/generics.py diff --git a/sprint5/generics.py b/sprint5/generics.py new file mode 100644 index 00000000..d52bab7d --- /dev/null +++ b/sprint5/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=10,children=[]) + +imran = Person(name="Imran", age=40,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 From ead9bfde2168f132d9812eccbf93895cd9dbb948 Mon Sep 17 00:00:00 2001 From: Fatma Degirmenci Date: Mon, 12 Jan 2026 21:52:11 +0000 Subject: [PATCH 7/8] Refactor Person to have preferred_operating_systems as List[str] and update find_possible_laptops --- sprint5/type-guided_refactorings.py | 42 +++++++++++++++++++++++++++++ 1 file changed, 42 insertions(+) create mode 100644 sprint5/type-guided_refactorings.py diff --git a/sprint5/type-guided_refactorings.py b/sprint5/type-guided_refactorings.py new file mode 100644 index 00000000..cfccc8e0 --- /dev/null +++ b/sprint5/type-guided_refactorings.py @@ -0,0 +1,42 @@ +from dataclasses import dataclass +from typing import List + +@dataclass(frozen=True) +class Person: + name: str + age: int + preferred_operating_system: 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 == person.preferred_operating_system: + possible_laptops.append(laptop) + return possible_laptops + + +people = [ + Person(name="Imran", age=22, preferred_operating_system=["Ubuntu","Arch Linux"]), + Person(name="Eliza", age=34, preferred_operating_system=["Ubuntu","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 From 0cc0f0448f78d0acf7a83cffb43c1d51786188d5 Mon Sep 17 00:00:00 2001 From: Fatma Degirmenci Date: Mon, 12 Jan 2026 21:53:11 +0000 Subject: [PATCH 8/8] Implement Parent and Child classes --- sprint5/inheritance.py | 37 +++++++++++++++++++++++++++++++++++++ 1 file changed, 37 insertions(+) create mode 100644 sprint5/inheritance.py diff --git a/sprint5/inheritance.py b/sprint5/inheritance.py new file mode 100644 index 00000000..339e0a16 --- /dev/null +++ b/sprint5/inheritance.py @@ -0,0 +1,37 @@ +class Parent: + def __init__(self, first_name: str, last_name: str): + self.first_name = first_name + self.last_name = last_name + + def get_name(self) -> str: + return f"{self.first_name} {self.last_name}" + + +class Child(Parent): + def __init__(self, first_name: str, last_name: str): + super().__init__(first_name, last_name) + self.previous_last_names = [] + + def change_last_name(self, last_name) -> None: + self.previous_last_names.append(self.last_name) + self.last_name = last_name + + def get_full_name(self) -> str: + suffix = "" + if len(self.previous_last_names) > 0: + suffix = f" (née {self.previous_last_names[0]})" + return f"{self.first_name} {self.last_name}{suffix}" + +person1 = Child("Elizaveta", "Alekseeva") +print(person1.get_name()) +print(person1.get_full_name()) +person1.change_last_name("Tyurina") +print(person1.get_name()) +print(person1.get_full_name()) + +person2 = Parent("Elizaveta", "Alekseeva") +print(person2.get_name()) +print(person2.get_full_name()) +person2.change_last_name("Tyurina") +print(person2.get_name()) +print(person2.get_full_name()) \ No newline at end of file