From 2090e74bad130ae8b248e2b422846d5c38f68927 Mon Sep 17 00:00:00 2001 From: Nataliia Volkova Date: Sat, 20 Dec 2025 19:34:52 +0000 Subject: [PATCH 1/6] 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 2/6] 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 3/6] 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 4/6] 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 5/6] 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 6/6] 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