From 9a18513278faf998f35b44bc461647264379f938 Mon Sep 17 00:00:00 2001 From: sarawone Date: Sat, 25 Oct 2025 11:45:58 +0100 Subject: [PATCH 1/9] Type Checking with mypy exercise --- AYK/typechecking_mypy.py | 30 ++++++++++++++++++++++++++++++ 1 file changed, 30 insertions(+) create mode 100644 AYK/typechecking_mypy.py diff --git a/AYK/typechecking_mypy.py b/AYK/typechecking_mypy.py new file mode 100644 index 0000000..c407442 --- /dev/null +++ b/AYK/typechecking_mypy.py @@ -0,0 +1,30 @@ +def open_account(balances, name, amount): + 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): + 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", 9.13) +open_account(balances,"Olya", "£7.13") + +total_pence = sum_balances(balances) +total_string = format_pence_as_string(total_pence) + +print(f"The bank accounts total {total_string}") \ No newline at end of file From f18bcf6ea581e037bf05eca90f752a8c74e73e2c Mon Sep 17 00:00:00 2001 From: sarawone Date: Sat, 25 Oct 2025 12:46:12 +0100 Subject: [PATCH 2/9] class&object ex2 --- .vscode/settings.json | 7 +++++++ AYK/class&object.py | 21 +++++++++++++++++++++ mypy.ini | 5 +++++ 3 files changed, 33 insertions(+) create mode 100644 .vscode/settings.json create mode 100644 AYK/class&object.py create mode 100644 mypy.ini diff --git a/.vscode/settings.json b/.vscode/settings.json new file mode 100644 index 0000000..c1cb9df --- /dev/null +++ b/.vscode/settings.json @@ -0,0 +1,7 @@ + + { + "python.linting.enabled": true, + "python.linting.mypyEnabled": true, + "python.linting.mypyArgs": ["--ignore-missing-imports"], + "python.analysis.typeCheckingMode": "basic" + } diff --git a/AYK/class&object.py b/AYK/class&object.py new file mode 100644 index 0000000..c774bba --- /dev/null +++ b/AYK/class&object.py @@ -0,0 +1,21 @@ +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) + +eliza = Person("Eliza", 34, "Arch Linux") +print(eliza.name) + + +def is_adult (person: Person) ->bool: + return person.age >=18 + +print(is_adult(imran)) + +def current_address (address : Person): + return address.location; + diff --git a/mypy.ini b/mypy.ini new file mode 100644 index 0000000..323ce15 --- /dev/null +++ b/mypy.ini @@ -0,0 +1,5 @@ +python_version = 3.9.6 +warn_return_any = True +warn_unused_configs = True +disallow_untyped_defs = True +ignore_missing_imports = True \ No newline at end of file From 523289a82a376cd715ccbaf5bcdee1fb71d1cce5 Mon Sep 17 00:00:00 2001 From: sarawone Date: Sat, 25 Oct 2025 13:36:11 +0100 Subject: [PATCH 3/9] method exercise --- AYK/method.py | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) create mode 100644 AYK/method.py diff --git a/AYK/method.py b/AYK/method.py new file mode 100644 index 0000000..aa89f0d --- /dev/null +++ b/AYK/method.py @@ -0,0 +1,18 @@ +from datetime import date + +class Person: + def __init__(self, name: str, dob: date, preferred_operating_system: str): + self.name = name + self.dob = dob + self.preferred_operating_system = preferred_operating_system + + + def is_adult (self) -> bool: + today = date.today() + age = today.year - self.dob.year - ((today.month,today.day)<(self.dob.month,self.dob.day)) + return age >=18 + +person1 = Person("AYK",date(1990,8,15),"mac") + +print (person1.name + " born on " + str(person1.dob)+ " and like " + person1.preferred_operating_system) +print(person1.is_adult()) \ No newline at end of file From 289488b2bf00834105b2deb4181947c998b95d78 Mon Sep 17 00:00:00 2001 From: sarawone Date: Sat, 25 Oct 2025 14:08:29 +0100 Subject: [PATCH 4/9] dataclass exercise --- AYK/dataclass.py | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) create mode 100644 AYK/dataclass.py diff --git a/AYK/dataclass.py b/AYK/dataclass.py new file mode 100644 index 0000000..d74f2d6 --- /dev/null +++ b/AYK/dataclass.py @@ -0,0 +1,19 @@ +from dataclasses import dataclass +from datetime import date + +@dataclass +class Person: + name: str + dob: date + preferred_operating_system: str + + def is_adult(self) -> bool: + today = date.today() + age = today.year - self.dob.year - ((today.month,today.day)<(self.dob.month,self.dob.day)) + return age >= 18 + +# Example usage +person1 = Person(name="AYK", dob=date(1989, 7, 15), preferred_operating_system="mac") + +print(person1.name) # AYK +print(person1.is_adult()) \ No newline at end of file From 87645d3dc972705ef85a08d8d3c9653bf88b7ce2 Mon Sep 17 00:00:00 2001 From: sarawone Date: Sat, 25 Oct 2025 14:18:43 +0100 Subject: [PATCH 5/9] generic ex --- AYK/generic.py | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) create mode 100644 AYK/generic.py diff --git a/AYK/generic.py b/AYK/generic.py new file mode 100644 index 0000000..893702a --- /dev/null +++ b/AYK/generic.py @@ -0,0 +1,21 @@ +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 = 4) +aisha = Person(name="Aisha", children=[],age =2) + +imran = Person(name="Imran", children=[fatma, aisha],age=33) + +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 6e1b00aaa231a3b904e7ae980bc3abdbd49067e5 Mon Sep 17 00:00:00 2001 From: sarawone Date: Sat, 25 Oct 2025 15:02:28 +0100 Subject: [PATCH 6/9] refactoring exercise --- AYK/refactorings.py | 42 ++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 42 insertions(+) create mode 100644 AYK/refactorings.py diff --git a/AYK/refactorings.py b/AYK/refactorings.py new file mode 100644 index 0000000..ab715b5 --- /dev/null +++ b/AYK/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_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 [os for os in person.preferred_operating_systems]: + possible_laptops.append(laptop) + return possible_laptops + + +people = [ + Person(name="Imran", age=22, preferred_operating_systems= ["Arch Linux","Ubuntu"]), + Person(name="Eliza", age=34, preferred_operating_systems=["macOS","Ubuntu"]), +] + +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 0c3a38de16d2cc152c954a4164ea2e85d1f57470 Mon Sep 17 00:00:00 2001 From: sarawone Date: Sat, 25 Oct 2025 15:39:29 +0100 Subject: [PATCH 7/9] enum exercise --- AYK/enum.py | 78 +++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 78 insertions(+) create mode 100644 AYK/enum.py diff --git a/AYK/enum.py b/AYK/enum.py new file mode 100644 index 0000000..d72587e --- /dev/null +++ b/AYK/enum.py @@ -0,0 +1,78 @@ +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 + +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] + + +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), +] + + +name = input("Enter your name: ").strip() +age_input = input("Enter your age: ").strip() +age = int(age_input) +if age <= 0: + print("Age must be positive") + print("Available Operating Systems: macOS, Arch Linux, Ubuntu") + os_input = input("Enter your preferred operating system: ").strip().lower() + + + os_map = {os.value.lower(): os for os in OperatingSystem} + if os_input not in os_map: + print(f"Invalid operating system '{os_input}'") + preferred_os = os_map[os_input] + + + sys.exit(1) + + +person = Person(name=name, age=age, preferred_operating_system=preferred_os) + + +matching_laptops = find_possible_laptops(laptops, person) +print(f"\nHello {person.name}! There are {len(matching_laptops)} laptop(s) with {person.preferred_operating_system.value} available.") + + +os_counts = {os: 0 for os in OperatingSystem} +for laptop in laptops: + os_counts[laptop.operating_system] += 1 + + +max_os = None +max_count = 0 + +for os, count in os_counts.items(): + if count > max_count: + max_os = os + max_count = count + +if max_os != person.preferred_operating_system and max_count > len(matching_laptops): + print(f"Tip: There are more laptops available with {max_os.value}.") \ No newline at end of file From adedd2e94e6c0de81cc04ea3c4da357df2421aa3 Mon Sep 17 00:00:00 2001 From: sarawone Date: Sat, 25 Oct 2025 16:55:05 +0100 Subject: [PATCH 8/9] allocation --- Allocation/allocatioin.py | 51 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 51 insertions(+) create mode 100644 Allocation/allocatioin.py diff --git a/Allocation/allocatioin.py b/Allocation/allocatioin.py new file mode 100644 index 0000000..002ccfb --- /dev/null +++ b/Allocation/allocatioin.py @@ -0,0 +1,51 @@ +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: List[OperatingSystem] + +@dataclass(frozen=True) +class Laptop: + id: int + manufacturer: str + model: str + screen_size_in_inches: float + operating_system: OperatingSystem + +def allocate_laptops(people: List[Person], laptops: List[Laptop]) -> Dict[Person, Laptop]: + laptops_remaining = laptops.copy() + allocation: Dict[Person, Laptop] = {} + + for person in people: + best_laptop = None + best_sadness = 101 + + + for laptop in laptops_remaining: + if laptop.operating_system in person.preferred_operating_system: + sadness = person.preferred_operating_system.index(laptop.operating_system) + else: + sadness = 100 + + if sadness < best_sadness: + best_sadness = sadness + best_laptop = laptop + + if best_laptop is None: + print(f"No laptops available to allocate to {person.name}") + + allocation[person.name] = best_laptop + laptops_remaining.remove(best_laptop) + + return allocation + + From cad3bb2308db6658f13c00334bfeb4aea0dc7755 Mon Sep 17 00:00:00 2001 From: sarawone Date: Sat, 25 Oct 2025 16:59:00 +0100 Subject: [PATCH 9/9] removing file --- Allocation/allocatioin.py | 51 --------------------------------------- 1 file changed, 51 deletions(-) delete mode 100644 Allocation/allocatioin.py diff --git a/Allocation/allocatioin.py b/Allocation/allocatioin.py deleted file mode 100644 index 002ccfb..0000000 --- a/Allocation/allocatioin.py +++ /dev/null @@ -1,51 +0,0 @@ -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: List[OperatingSystem] - -@dataclass(frozen=True) -class Laptop: - id: int - manufacturer: str - model: str - screen_size_in_inches: float - operating_system: OperatingSystem - -def allocate_laptops(people: List[Person], laptops: List[Laptop]) -> Dict[Person, Laptop]: - laptops_remaining = laptops.copy() - allocation: Dict[Person, Laptop] = {} - - for person in people: - best_laptop = None - best_sadness = 101 - - - for laptop in laptops_remaining: - if laptop.operating_system in person.preferred_operating_system: - sadness = person.preferred_operating_system.index(laptop.operating_system) - else: - sadness = 100 - - if sadness < best_sadness: - best_sadness = sadness - best_laptop = laptop - - if best_laptop is None: - print(f"No laptops available to allocate to {person.name}") - - allocation[person.name] = best_laptop - laptops_remaining.remove(best_laptop) - - return allocation - -