From 2090e74bad130ae8b248e2b422846d5c38f68927 Mon Sep 17 00:00:00 2001 From: Nataliia Volkova Date: Sat, 20 Dec 2025 19:34:52 +0000 Subject: [PATCH 01/11] Inheritens --- inherit.py | 39 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 39 insertions(+) create mode 100644 inherit.py diff --git a/inherit.py b/inherit.py new file mode 100644 index 00000000..f0675ef4 --- /dev/null +++ b/inherit.py @@ -0,0 +1,39 @@ +from typing import List + +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: List [str]= [] + + def change_last_name(self, last_name: str) -> None: + self.previous_last_names.append(self.last_name) + self.last_name = last_name + + def get_full_name(self) -> str: + suffix: str = "" + 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()) // this method dose not belong to Parent class +# person2.change_last_name("Tyurina") // this method dose not belong to Parent class +print(person2.get_name()) +# print(person2.get_full_name()) // this method dose not belong to Parent class \ No newline at end of file From 7e0352ea549785b15892efaebf7c718f8d492f21 Mon Sep 17 00:00:00 2001 From: Nataliia Volkova Date: Sat, 20 Dec 2025 19:46:34 +0000 Subject: [PATCH 02/11] enum exer. --- enums.py | 105 +++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 105 insertions(+) create mode 100644 enums.py diff --git a/enums.py b/enums.py new file mode 100644 index 00000000..2a776b91 --- /dev/null +++ b/enums.py @@ -0,0 +1,105 @@ +from dataclasses import dataclass +from enum import Enum +from typing import List, Dict +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 + + +def count_laptops(laptops: List[Laptop]) -> Dict[OperatingSystem, int]: + number_eachOS_laptops: Dict[OperatingSystem, int] = { + OperatingSystem.MACOS: 0, + OperatingSystem.ARCH: 0, + OperatingSystem.UBUNTU: 0} + for laptop in laptops: + number_eachOS_laptops[laptop.operating_system] +=1 + return number_eachOS_laptops + + +def count_possible_laptops(laptops: List[Laptop], person: Person) -> int: + possible_laptops: List[Laptop] =[] + for laptop in laptops: + if laptop.operating_system == person.preferred_operating_system: + possible_laptops.append(laptop) + number_possible_laptops = len(possible_laptops) + return number_possible_laptops + +def chose_alternative_laptops(laptops: List[Laptop], person: Person) -> Dict[OperatingSystem, int]: + number_possible_laptops = count_possible_laptops(laptops, person) + number_eachOS_laptops = count_laptops(laptops) + preferred_os = person.preferred_operating_system + alternative_laptops: Dict[OperatingSystem, int] = {} + for eachOS, count in number_eachOS_laptops.items(): + if eachOS == preferred_os: + continue + if count > number_possible_laptops: + alternative_laptops[eachOS] = count + if len(alternative_laptops) != 0: + print(f"There is an operating system that has more laptops available.If you’re willing to accept them, there is a list: {alternative_laptops}.") + return alternative_laptops + else: + print("There is not an operating system that has more laptops available.") + return alternative_laptops + +while True: + user_name = input("Type your name: ").strip() + if len(user_name) < 3: + print(f"Error, {user_name} is not valid. Try again, length should be more than 3 characters.") + continue + break + +while True: + user_age = input("Type your age: ").strip() + try: + user_age_int = int(user_age) + if user_age_int < 18: + raise ValueError + break + except ValueError: + print("Invalid age, try again! Borrowing allowed from 18 years old.") + +available_os = [os.value for os in OperatingSystem] +print("Available OSs are: ", ",".join(available_os)) +user_operating_system = input("Type operating system: ").strip() +if user_operating_system not in available_os: + print(f"Error, {user_operating_system} is not in available list\n" + f"Available OSs are: {','.join(available_os)}", file=sys.stderr) + sys.exit(1) + +preferred_operating_system = OperatingSystem(user_operating_system) + +user = Person(name=user_name, age=user_age_int, preferred_operating_system=preferred_operating_system) + + +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), +] + + +possible_laptops = count_possible_laptops(laptops, user) +print(f"Possible laptops for {user_name}: {possible_laptops}") +alternative_laptops = chose_alternative_laptops(laptops, user) From 5cb4ece04ca5a3b176e2d174f197f576935de0d9 Mon Sep 17 00:00:00 2001 From: Nataliia Volkova Date: Sat, 20 Dec 2025 19:54:53 +0000 Subject: [PATCH 03/11] typy guide refactoring --- type_guide_refact.py | 42 ++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 42 insertions(+) create mode 100644 type_guide_refact.py diff --git a/type_guide_refact.py b/type_guide_refact.py new file mode 100644 index 00000000..f1ee15eb --- /dev/null +++ b/type_guide_refact.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: list[Laptop] = [] + 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 From dedfc1110bf251aeb4af747d25a844820fc2929d Mon Sep 17 00:00:00 2001 From: Nataliia Volkova Date: Sat, 20 Dec 2025 20:02:10 +0000 Subject: [PATCH 04/11] generics --- familytree.py | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) create mode 100644 familytree.py diff --git a/familytree.py b/familytree.py new file mode 100644 index 00000000..2e2579d6 --- /dev/null +++ b/familytree.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", children=[], age=17) +aisha = Person(name="Aisha", children=[], age=25) + +imran = Person(name="Imran", children=[fatma, aisha], age=51) + +def print_family_tree(person: Person) -> None: + print(person.name, f"({person.age})") + for child in person.children: + print(f"- {child.name} ({child.age})") + +print_family_tree(imran) \ No newline at end of file From 416bc20c494de27bbad3618abe2aa3b1a88ba873 Mon Sep 17 00:00:00 2001 From: Nataliia Volkova Date: Sat, 20 Dec 2025 20:10:41 +0000 Subject: [PATCH 05/11] dataclasses --- dataclasses_ex.py | 24 ++++++++++++++++++++++++ 1 file changed, 24 insertions(+) create mode 100644 dataclasses_ex.py diff --git a/dataclasses_ex.py b/dataclasses_ex.py new file mode 100644 index 00000000..0114071a --- /dev/null +++ b/dataclasses_ex.py @@ -0,0 +1,24 @@ +from datetime import date +from dataclasses import dataclass + +@dataclass(frozen=True) +class Person: + name: str + preferred_operating_system: str + birth_date: date + + def is_adult(self) -> bool: + today = date.today() + age = today.year - self.birth_date.year + + if (today.month, today.day) < (self.birth_date.month, self.birth_date.day): + age -=1 + + return age >= 18 + +imran = Person("Imran", "Ubuntu", date(2000, 9, 12)) + +print(imran.is_adult()) + + + From 0f1a924b5af1b685e282cb4e00ea8a4839ee1a8f Mon Sep 17 00:00:00 2001 From: Nataliia Volkova Date: Sat, 20 Dec 2025 20:54:09 +0000 Subject: [PATCH 06/11] types_banc_account_exer --- typesExer.py | 33 +++++++++++++++++++++++++++++++++ 1 file changed, 33 insertions(+) create mode 100644 typesExer.py diff --git a/typesExer.py b/typesExer.py new file mode 100644 index 00000000..9ba857ef --- /dev/null +++ b/typesExer.py @@ -0,0 +1,33 @@ +from typing import Union, Dict + + +def open_account(balances: Dict[str, int], name: str, amount: Union[str, float]): + balances[name] = int(float(amount) *100) + +def sum_balances(accounts: Dict[str, int]): + total = 0 + for name, pence in accounts.items(): + print(f"{name} had balance {pence}") + total += pence + return total + +def format_pence_as_pound(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 = { + "Sima": 700, + "Linn": 545, + "Georg": 831, +} + +open_account(balances, "Tobi", 9.13) +open_account(balances, "Olya", 7.13) + +total_pence = sum_balances(balances) +total_pound = format_pence_as_pound(total_pence) + +print(f"The bank accounts total {total_pound}") \ No newline at end of file From 1ad7eaf9b5051611a0c6f49217ca4d521c2a0368 Mon Sep 17 00:00:00 2001 From: Nataliia Volkova Date: Wed, 14 Jan 2026 20:48:53 +0000 Subject: [PATCH 07/11] recursion --- familytree.py | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/familytree.py b/familytree.py index 2e2579d6..bf133fd4 100644 --- a/familytree.py +++ b/familytree.py @@ -17,4 +17,11 @@ def print_family_tree(person: Person) -> None: for child in person.children: print(f"- {child.name} ({child.age})") -print_family_tree(imran) \ No newline at end of file +# recursion + +def print_family_tree_recursion(person: Person, level:str = "¬") -> None: + print(f"{level} {person.name} ({person.age})") + for child in person.children: + print_family_tree_recursion(child, level + "|-") + +print_family_tree_recursion(fatma) \ No newline at end of file From 6b222a1295b8930b4d6764e9dd43c96aca6aa595 Mon Sep 17 00:00:00 2001 From: Nataliia Volkova Date: Wed, 14 Jan 2026 21:56:11 +0000 Subject: [PATCH 08/11] cleaner printing and cleaner output --- enums.py | 19 ++++++++++++++----- 1 file changed, 14 insertions(+), 5 deletions(-) diff --git a/enums.py b/enums.py index 2a776b91..0d9d813e 100644 --- a/enums.py +++ b/enums.py @@ -56,7 +56,11 @@ def chose_alternative_laptops(laptops: List[Laptop], person: Person) -> Dict[Ope if count > number_possible_laptops: alternative_laptops[eachOS] = count if len(alternative_laptops) != 0: - print(f"There is an operating system that has more laptops available.If you’re willing to accept them, there is a list: {alternative_laptops}.") + print("There is an operating system that has more laptops available.If you’re willing to accept them, there is a list:") + list_os = [] + for os, count in alternative_laptops.items(): + list_os.append(f"{os.value}: {count}") + print(" ,".join(list_os)) return alternative_laptops else: print("There is not an operating system that has more laptops available.") @@ -81,9 +85,14 @@ def chose_alternative_laptops(laptops: List[Laptop], person: Person) -> Dict[Ope available_os = [os.value for os in OperatingSystem] print("Available OSs are: ", ",".join(available_os)) -user_operating_system = input("Type operating system: ").strip() -if user_operating_system not in available_os: - print(f"Error, {user_operating_system} is not in available list\n" +while True: + user_operating_system = input("Type operating system: ").strip() + try: + if user_operating_system not in available_os: + raise ValueError + break + except ValueError: + print(f"Error, {user_operating_system} is not in available list\n" f"Available OSs are: {','.join(available_os)}", file=sys.stderr) sys.exit(1) @@ -99,7 +108,7 @@ def chose_alternative_laptops(laptops: List[Laptop], person: Person) -> Dict[Ope Laptop(id=4, manufacturer="Apple", model="macBook", screen_size_in_inches=13, operating_system=OperatingSystem.MACOS), ] - possible_laptops = count_possible_laptops(laptops, user) print(f"Possible laptops for {user_name}: {possible_laptops}") alternative_laptops = chose_alternative_laptops(laptops, user) + From 75f4c5b87400a99a1438118d18a9fae66ac557c5 Mon Sep 17 00:00:00 2001 From: Nataliia Volkova Date: Mon, 19 Jan 2026 11:43:48 +0000 Subject: [PATCH 09/11] resolved an issue about validating the OSes --- enums.py | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/enums.py b/enums.py index 0d9d813e..a470d264 100644 --- a/enums.py +++ b/enums.py @@ -93,8 +93,8 @@ def chose_alternative_laptops(laptops: List[Laptop], person: Person) -> Dict[Ope break except ValueError: print(f"Error, {user_operating_system} is not in available list\n" - f"Available OSs are: {','.join(available_os)}", file=sys.stderr) - sys.exit(1) + f"Available OSs are: {','.join(available_os)}. Try again!.", file=sys.stderr) + preferred_operating_system = OperatingSystem(user_operating_system) @@ -111,4 +111,3 @@ def chose_alternative_laptops(laptops: List[Laptop], person: Person) -> Dict[Ope possible_laptops = count_possible_laptops(laptops, user) print(f"Possible laptops for {user_name}: {possible_laptops}") alternative_laptops = chose_alternative_laptops(laptops, user) - From 1510d2b1ead3f3931ee59429c5a136fab85d0572 Mon Sep 17 00:00:00 2001 From: Nataliia Volkova Date: Mon, 19 Jan 2026 12:15:07 +0000 Subject: [PATCH 10/11] moving files from the main directory to a separate folder --- prep-exercises/dataclasses_ex.py | 24 ++++++ prep-exercises/enums.py | 113 ++++++++++++++++++++++++++++ prep-exercises/familytree.py | 27 +++++++ prep-exercises/inherit.py | 39 ++++++++++ prep-exercises/type_guide_refact.py | 42 +++++++++++ prep-exercises/typesExer.py | 33 ++++++++ 6 files changed, 278 insertions(+) create mode 100644 prep-exercises/dataclasses_ex.py create mode 100644 prep-exercises/enums.py create mode 100644 prep-exercises/familytree.py create mode 100644 prep-exercises/inherit.py create mode 100644 prep-exercises/type_guide_refact.py create mode 100644 prep-exercises/typesExer.py diff --git a/prep-exercises/dataclasses_ex.py b/prep-exercises/dataclasses_ex.py new file mode 100644 index 00000000..0114071a --- /dev/null +++ b/prep-exercises/dataclasses_ex.py @@ -0,0 +1,24 @@ +from datetime import date +from dataclasses import dataclass + +@dataclass(frozen=True) +class Person: + name: str + preferred_operating_system: str + birth_date: date + + def is_adult(self) -> bool: + today = date.today() + age = today.year - self.birth_date.year + + if (today.month, today.day) < (self.birth_date.month, self.birth_date.day): + age -=1 + + return age >= 18 + +imran = Person("Imran", "Ubuntu", date(2000, 9, 12)) + +print(imran.is_adult()) + + + diff --git a/prep-exercises/enums.py b/prep-exercises/enums.py new file mode 100644 index 00000000..aa32e0c2 --- /dev/null +++ b/prep-exercises/enums.py @@ -0,0 +1,113 @@ +from dataclasses import dataclass +from enum import Enum +from typing import List, Dict + + + + + +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 + + +def count_laptops(laptops: List[Laptop]) -> Dict[OperatingSystem, int]: + number_eachOS_laptops: Dict[OperatingSystem, int] = { + OperatingSystem.MACOS: 0, + OperatingSystem.ARCH: 0, + OperatingSystem.UBUNTU: 0} + for laptop in laptops: + number_eachOS_laptops[laptop.operating_system] +=1 + return number_eachOS_laptops + + +def count_possible_laptops(laptops: List[Laptop], person: Person) -> int: + possible_laptops: List[Laptop] =[] + for laptop in laptops: + if laptop.operating_system == person.preferred_operating_system: + possible_laptops.append(laptop) + number_possible_laptops = len(possible_laptops) + return number_possible_laptops + +def chose_alternative_laptops(laptops: List[Laptop], person: Person) -> Dict[OperatingSystem, int]: + number_possible_laptops = count_possible_laptops(laptops, person) + number_eachOS_laptops = count_laptops(laptops) + preferred_os = person.preferred_operating_system + alternative_laptops: Dict[OperatingSystem, int] = {} + for eachOS, count in number_eachOS_laptops.items(): + if eachOS == preferred_os: + continue + if count > number_possible_laptops: + alternative_laptops[eachOS] = count + if len(alternative_laptops) != 0: + print("There is an operating system that has more laptops available.If you’re willing to accept them, there is a list:") + list_os = [] + for os, count in alternative_laptops.items(): + list_os.append(f"{os.value}: {count}") + print(" ,".join(list_os)) + return alternative_laptops + else: + print("There is not an operating system that has more laptops available.") + return alternative_laptops + +while True: + user_name = input("Type your name: ").strip() + if len(user_name) < 3: + print(f"Error, {user_name} is not valid. Try again, length should be more than 3 characters.") + continue + break + +while True: + user_age = input("Type your age: ").strip() + try: + user_age_int = int(user_age) + if user_age_int < 18: + raise ValueError + break + except ValueError: + print("Invalid age, try again! Borrowing allowed from 18 years old.") + +available_os = [os.value for os in OperatingSystem] +print("Available OSs are: ", ",".join(available_os)) +while True: + user_operating_system = input("Type operating system: ").strip() + try: + if user_operating_system not in available_os: + raise ValueError + break + except ValueError: + print(f"Error, {user_operating_system} is not in available list\n" + f"Available OSs are: {','.join(available_os)}. Try again!.", file=sys.stderr) + + +preferred_operating_system = OperatingSystem(user_operating_system) + +user = Person(name=user_name, age=user_age_int, preferred_operating_system=preferred_operating_system) + + +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), +] + +possible_laptops = count_possible_laptops(laptops, user) +print(f"Possible laptops for {user_name}: {possible_laptops}") +alternative_laptops = chose_alternative_laptops(laptops, user) diff --git a/prep-exercises/familytree.py b/prep-exercises/familytree.py new file mode 100644 index 00000000..bf133fd4 --- /dev/null +++ b/prep-exercises/familytree.py @@ -0,0 +1,27 @@ +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=17) +aisha = Person(name="Aisha", children=[], age=25) + +imran = Person(name="Imran", children=[fatma, aisha], age=51) + +def print_family_tree(person: Person) -> None: + print(person.name, f"({person.age})") + for child in person.children: + print(f"- {child.name} ({child.age})") + +# recursion + +def print_family_tree_recursion(person: Person, level:str = "¬") -> None: + print(f"{level} {person.name} ({person.age})") + for child in person.children: + print_family_tree_recursion(child, level + "|-") + +print_family_tree_recursion(fatma) \ No newline at end of file diff --git a/prep-exercises/inherit.py b/prep-exercises/inherit.py new file mode 100644 index 00000000..f0675ef4 --- /dev/null +++ b/prep-exercises/inherit.py @@ -0,0 +1,39 @@ +from typing import List + +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: List [str]= [] + + def change_last_name(self, last_name: str) -> None: + self.previous_last_names.append(self.last_name) + self.last_name = last_name + + def get_full_name(self) -> str: + suffix: str = "" + 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()) // this method dose not belong to Parent class +# person2.change_last_name("Tyurina") // this method dose not belong to Parent class +print(person2.get_name()) +# print(person2.get_full_name()) // this method dose not belong to Parent class \ No newline at end of file diff --git a/prep-exercises/type_guide_refact.py b/prep-exercises/type_guide_refact.py new file mode 100644 index 00000000..f1ee15eb --- /dev/null +++ b/prep-exercises/type_guide_refact.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: list[Laptop] = [] + 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/typesExer.py b/prep-exercises/typesExer.py new file mode 100644 index 00000000..9ba857ef --- /dev/null +++ b/prep-exercises/typesExer.py @@ -0,0 +1,33 @@ +from typing import Union, Dict + + +def open_account(balances: Dict[str, int], name: str, amount: Union[str, float]): + balances[name] = int(float(amount) *100) + +def sum_balances(accounts: Dict[str, int]): + total = 0 + for name, pence in accounts.items(): + print(f"{name} had balance {pence}") + total += pence + return total + +def format_pence_as_pound(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 = { + "Sima": 700, + "Linn": 545, + "Georg": 831, +} + +open_account(balances, "Tobi", 9.13) +open_account(balances, "Olya", 7.13) + +total_pence = sum_balances(balances) +total_pound = format_pence_as_pound(total_pence) + +print(f"The bank accounts total {total_pound}") \ No newline at end of file From 0eac4eae69a5d92d4f3524618636b377774d4620 Mon Sep 17 00:00:00 2001 From: Nataliia Volkova Date: Mon, 19 Jan 2026 12:31:34 +0000 Subject: [PATCH 11/11] moving files from the main directory to a separate folder --- dataclasses_ex.py | 24 --------- enums.py | 113 ------------------------------------------- familytree.py | 27 ----------- inherit.py | 39 --------------- type_guide_refact.py | 42 ---------------- typesExer.py | 33 ------------- 6 files changed, 278 deletions(-) delete mode 100644 dataclasses_ex.py delete mode 100644 enums.py delete mode 100644 familytree.py delete mode 100644 inherit.py delete mode 100644 type_guide_refact.py delete mode 100644 typesExer.py diff --git a/dataclasses_ex.py b/dataclasses_ex.py deleted file mode 100644 index 0114071a..00000000 --- a/dataclasses_ex.py +++ /dev/null @@ -1,24 +0,0 @@ -from datetime import date -from dataclasses import dataclass - -@dataclass(frozen=True) -class Person: - name: str - preferred_operating_system: str - birth_date: date - - def is_adult(self) -> bool: - today = date.today() - age = today.year - self.birth_date.year - - if (today.month, today.day) < (self.birth_date.month, self.birth_date.day): - age -=1 - - return age >= 18 - -imran = Person("Imran", "Ubuntu", date(2000, 9, 12)) - -print(imran.is_adult()) - - - diff --git a/enums.py b/enums.py deleted file mode 100644 index a470d264..00000000 --- a/enums.py +++ /dev/null @@ -1,113 +0,0 @@ -from dataclasses import dataclass -from enum import Enum -from typing import List, Dict -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 - - -def count_laptops(laptops: List[Laptop]) -> Dict[OperatingSystem, int]: - number_eachOS_laptops: Dict[OperatingSystem, int] = { - OperatingSystem.MACOS: 0, - OperatingSystem.ARCH: 0, - OperatingSystem.UBUNTU: 0} - for laptop in laptops: - number_eachOS_laptops[laptop.operating_system] +=1 - return number_eachOS_laptops - - -def count_possible_laptops(laptops: List[Laptop], person: Person) -> int: - possible_laptops: List[Laptop] =[] - for laptop in laptops: - if laptop.operating_system == person.preferred_operating_system: - possible_laptops.append(laptop) - number_possible_laptops = len(possible_laptops) - return number_possible_laptops - -def chose_alternative_laptops(laptops: List[Laptop], person: Person) -> Dict[OperatingSystem, int]: - number_possible_laptops = count_possible_laptops(laptops, person) - number_eachOS_laptops = count_laptops(laptops) - preferred_os = person.preferred_operating_system - alternative_laptops: Dict[OperatingSystem, int] = {} - for eachOS, count in number_eachOS_laptops.items(): - if eachOS == preferred_os: - continue - if count > number_possible_laptops: - alternative_laptops[eachOS] = count - if len(alternative_laptops) != 0: - print("There is an operating system that has more laptops available.If you’re willing to accept them, there is a list:") - list_os = [] - for os, count in alternative_laptops.items(): - list_os.append(f"{os.value}: {count}") - print(" ,".join(list_os)) - return alternative_laptops - else: - print("There is not an operating system that has more laptops available.") - return alternative_laptops - -while True: - user_name = input("Type your name: ").strip() - if len(user_name) < 3: - print(f"Error, {user_name} is not valid. Try again, length should be more than 3 characters.") - continue - break - -while True: - user_age = input("Type your age: ").strip() - try: - user_age_int = int(user_age) - if user_age_int < 18: - raise ValueError - break - except ValueError: - print("Invalid age, try again! Borrowing allowed from 18 years old.") - -available_os = [os.value for os in OperatingSystem] -print("Available OSs are: ", ",".join(available_os)) -while True: - user_operating_system = input("Type operating system: ").strip() - try: - if user_operating_system not in available_os: - raise ValueError - break - except ValueError: - print(f"Error, {user_operating_system} is not in available list\n" - f"Available OSs are: {','.join(available_os)}. Try again!.", file=sys.stderr) - - -preferred_operating_system = OperatingSystem(user_operating_system) - -user = Person(name=user_name, age=user_age_int, preferred_operating_system=preferred_operating_system) - - -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), -] - -possible_laptops = count_possible_laptops(laptops, user) -print(f"Possible laptops for {user_name}: {possible_laptops}") -alternative_laptops = chose_alternative_laptops(laptops, user) diff --git a/familytree.py b/familytree.py deleted file mode 100644 index bf133fd4..00000000 --- a/familytree.py +++ /dev/null @@ -1,27 +0,0 @@ -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=17) -aisha = Person(name="Aisha", children=[], age=25) - -imran = Person(name="Imran", children=[fatma, aisha], age=51) - -def print_family_tree(person: Person) -> None: - print(person.name, f"({person.age})") - for child in person.children: - print(f"- {child.name} ({child.age})") - -# recursion - -def print_family_tree_recursion(person: Person, level:str = "¬") -> None: - print(f"{level} {person.name} ({person.age})") - for child in person.children: - print_family_tree_recursion(child, level + "|-") - -print_family_tree_recursion(fatma) \ No newline at end of file diff --git a/inherit.py b/inherit.py deleted file mode 100644 index f0675ef4..00000000 --- a/inherit.py +++ /dev/null @@ -1,39 +0,0 @@ -from typing import List - -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: List [str]= [] - - def change_last_name(self, last_name: str) -> None: - self.previous_last_names.append(self.last_name) - self.last_name = last_name - - def get_full_name(self) -> str: - suffix: str = "" - 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()) // this method dose not belong to Parent class -# person2.change_last_name("Tyurina") // this method dose not belong to Parent class -print(person2.get_name()) -# print(person2.get_full_name()) // this method dose not belong to Parent class \ No newline at end of file diff --git a/type_guide_refact.py b/type_guide_refact.py deleted file mode 100644 index f1ee15eb..00000000 --- a/type_guide_refact.py +++ /dev/null @@ -1,42 +0,0 @@ -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: list[Laptop] = [] - 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/typesExer.py b/typesExer.py deleted file mode 100644 index 9ba857ef..00000000 --- a/typesExer.py +++ /dev/null @@ -1,33 +0,0 @@ -from typing import Union, Dict - - -def open_account(balances: Dict[str, int], name: str, amount: Union[str, float]): - balances[name] = int(float(amount) *100) - -def sum_balances(accounts: Dict[str, int]): - total = 0 - for name, pence in accounts.items(): - print(f"{name} had balance {pence}") - total += pence - return total - -def format_pence_as_pound(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 = { - "Sima": 700, - "Linn": 545, - "Georg": 831, -} - -open_account(balances, "Tobi", 9.13) -open_account(balances, "Olya", 7.13) - -total_pence = sum_balances(balances) -total_pound = format_pence_as_pound(total_pence) - -print(f"The bank accounts total {total_pound}") \ No newline at end of file