From 35cb2cb8ae891b72dd2ad9c753b088b207adedf5 Mon Sep 17 00:00:00 2001 From: sheetalkharab Date: Sat, 11 Oct 2025 20:46:32 +0100 Subject: [PATCH 01/19] add double.py with answers --- double.py | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) create mode 100644 double.py diff --git a/double.py b/double.py new file mode 100644 index 0000000..0ef66d7 --- /dev/null +++ b/double.py @@ -0,0 +1,17 @@ +def half(value): + return value / 2 + +def double(value): + return value * 2 + +def second(value): + return value[1] + + + +print(double(22)) +print(double("hello")) +print(double("22")) + +# In Python when we multiply a string by a number, it repeats the string that many times(in our case it is 2 times). +# i got in answer 2222 \ No newline at end of file From 239f5007fe0a357151434f0c4fe273787b91d0a9 Mon Sep 17 00:00:00 2001 From: sheetalkharab Date: Sat, 11 Oct 2025 20:48:05 +0100 Subject: [PATCH 02/19] add and update bankAccount.py code with fixing error --- bankAccount.py | 32 ++++++++++++++++++++++++++++++++ 1 file changed, 32 insertions(+) create mode 100644 bankAccount.py diff --git a/bankAccount.py b/bankAccount.py new file mode 100644 index 0000000..eaf5509 --- /dev/null +++ b/bankAccount.py @@ -0,0 +1,32 @@ +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, +} +#missed balance argument, add balances +open_account(balances,"Tobi", 913) +#3rd argument is an int not a String and format as pence +open_account(balances,"Olya", 713) + +total_pence = sum_balances(balances) +#wrong name of function +total_string = format_pence_as_string(total_pence) + +print(f"The bank accounts total {total_string}") \ No newline at end of file From cb6fadcd47b781dd4b4fd87f7830e1d209b3b981 Mon Sep 17 00:00:00 2001 From: sheetalkharab Date: Sat, 11 Oct 2025 20:50:01 +0100 Subject: [PATCH 03/19] add new function that access a property which does not exist --- person.py | 29 +++++++++++++++++++++++++++++ 1 file changed, 29 insertions(+) create mode 100644 person.py diff --git a/person.py b/person.py new file mode 100644 index 0000000..1ad1a24 --- /dev/null +++ b/person.py @@ -0,0 +1,29 @@ +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) +#when try to run this code says person don't have address attribute +#need to comment out to run code +#print(imran.address) + +eliza = Person("Eliza", 34, "Arch Linux") +print(eliza.name) +#when try to run this code says person don't have address attribute +#need to commentout to run code +#print(eliza.address) + + +def is_adult(person: Person) -> bool: + return person.age >= 18 + +print(is_adult(imran)) + +# error: "Person" has no attribute "surname" +def get_surname(person: Person) -> str: + return person.surname + +print(get_surname(eliza)) \ No newline at end of file From 2d7f0a4af1f5279fe698d73964696d7086e60f35 Mon Sep 17 00:00:00 2001 From: sheetalkharab Date: Sat, 11 Oct 2025 21:28:32 +0100 Subject: [PATCH 04/19] add date_of_birth parameter in person class and update is_adult method --- person.py | 36 +++++++++++++++++------------------- 1 file changed, 17 insertions(+), 19 deletions(-) diff --git a/person.py b/person.py index 1ad1a24..2f2cc9d 100644 --- a/person.py +++ b/person.py @@ -1,29 +1,27 @@ +from datetime import date + + class Person: - def __init__(self, name: str, age: int, preferred_operating_system: str): + # a constructor with a new instance of class + def __init__(self, name: str, date_of_birth:date, preferred_operating_system: str): self.name = name - self.age = age + self.date_of_birth = date_of_birth self.preferred_operating_system = preferred_operating_system -imran = Person("Imran", 22, "Ubuntu") -print(imran.name) -#when try to run this code says person don't have address attribute -#need to comment out to run code -#print(imran.address) + def is_adult(self): + today_date = date.today() + birth_date= self.date_of_birth + #to check date and month as well + age = today_date.year-birth_date.year- ((today_date.month, today_date.day) < (birth_date.month, birth_date.day)) + return age>=18 -eliza = Person("Eliza", 34, "Arch Linux") -print(eliza.name) -#when try to run this code says person don't have address attribute -#need to commentout to run code -#print(eliza.address) +imran = Person("Imran", date(1989,6,25), "Ubuntu") +print(imran.name) +print(imran.is_adult()) -def is_adult(person: Person) -> bool: - return person.age >= 18 +eliza = Person("Eliza", date(2007,10,12), "Arch Linux") +print(eliza.is_adult()) -print(is_adult(imran)) -# error: "Person" has no attribute "surname" -def get_surname(person: Person) -> str: - return person.surname -print(get_surname(eliza)) \ No newline at end of file From 5b25f8b2c4fd32ee9b7a510c05d4ce4ed6cd33af Mon Sep 17 00:00:00 2001 From: sheetalkharab Date: Sat, 11 Oct 2025 21:59:12 +0100 Subject: [PATCH 05/19] write person class with help of dataclass decorator --- dataclassPerson.py | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) create mode 100644 dataclassPerson.py diff --git a/dataclassPerson.py b/dataclassPerson.py new file mode 100644 index 0000000..524a569 --- /dev/null +++ b/dataclassPerson.py @@ -0,0 +1,21 @@ +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): + today_date = date.today() + birth_date= self.date_of_birth + #to check date and month as well + age = today_date.year-birth_date.year- ((today_date.month, today_date.day) < (birth_date.month, birth_date.day)) + return age>=18 + +imran = Person("Imran", (1989,6,25), "Ubuntu") # We can call this constructor - @dataclass generated it for us. +print(imran) # Prints Person(name='Imran', date_of_birth=(1989, 6, 25), preferred_operating_system='Ubuntu') + +imran2 = Person("Imran", (1989,6,25), "Ubuntu") +print(imran == imran2) # Prints True \ No newline at end of file From 2235109ab5b3333eaafde0747d0f8d70384db066 Mon Sep 17 00:00:00 2001 From: sheetalkharab Date: Sat, 11 Oct 2025 22:15:21 +0100 Subject: [PATCH 06/19] add generic for type checking --- generic.py | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) create mode 100644 generic.py diff --git a/generic.py b/generic.py new file mode 100644 index 0000000..540f8db --- /dev/null +++ b/generic.py @@ -0,0 +1,20 @@ +from dataclasses import dataclass +from typing import List + +@dataclass(frozen=True) +class Person: + name: str + age: int + children: List["Person"] + +fatma = Person(name="Fatma",age=11, children=[]) +aisha = Person(name="Aisha", age=13,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 efe7029be0bdc919792ebf3402ceb813154a1cdf Mon Sep 17 00:00:00 2001 From: sheetalkharab Date: Sat, 11 Oct 2025 22:31:34 +0100 Subject: [PATCH 07/19] update type of preferred_operating_systems from str to list[str] --- laptopsallocate.py | 42 ++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 42 insertions(+) create mode 100644 laptopsallocate.py diff --git a/laptopsallocate.py b/laptopsallocate.py new file mode 100644 index 0000000..fa61f05 --- /dev/null +++ b/laptopsallocate.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 == 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 d2fb86da3828f304dafb6b43c322bb2584f79256 Mon Sep 17 00:00:00 2001 From: sheetalkharab Date: Sun, 12 Oct 2025 16:52:16 +0100 Subject: [PATCH 08/19] comment out line to remove error from inheritence.py --- inheritence.py | 43 +++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 43 insertions(+) create mode 100644 inheritence.py diff --git a/inheritence.py b/inheritence.py new file mode 100644 index 0000000..b7921c3 --- /dev/null +++ b/inheritence.py @@ -0,0 +1,43 @@ + +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) + # for type annotation as its a list of string + self.previous_last_names : List[str]= [] + + 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()) +# Need to Commented out because the Parent class doesn't have 'get_full_name()' and 'change_last_name()' methods +#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 From c7955bf5ed77ac88449d651e4a80e672b2c4c1f5 Mon Sep 17 00:00:00 2001 From: sheetalkharab Date: Sun, 12 Oct 2025 17:03:06 +0100 Subject: [PATCH 09/19] changes in laptop allocate --- enums.py | 128 +++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 128 insertions(+) create mode 100644 enums.py diff --git a/enums.py b/enums.py new file mode 100644 index 0000000..f49307f --- /dev/null +++ b/enums.py @@ -0,0 +1,128 @@ +from dataclasses import dataclass +from enum import Enum +import sys +from typing import List, Optional + +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]: + possible_laptops = [] + for laptop in laptops: + if laptop.operating_system == person.preferred_operating_system: + possible_laptops.append(laptop) + return possible_laptops + +def parse_operating_system(s: str) -> Optional[OperatingSystem]: + s_normal = s.strip().lower() + for os in OperatingSystem: + if os.value.lower() == s_normal: + return os + return None + + +people = [ + Person(name="Imran", age=22, preferred_operating_system=OperatingSystem.UBUNTU), + Person(name="Eliza", age=34, preferred_operating_system=OperatingSystem.ARCH), +] + +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), +] + + + +while True: + name = input("Enter your name: ") + if name == "": + print("Error: Name cannot be empty. Please try again.") + else: + break + +while True: + age_input = input("Enter your age: ") + if not age_input.isdigit(): + print("Error: Age must be a number.", file=sys.stderr) + continue + age = int(age_input) + if age < 0: + print("Error: Age cannot be negative.", file=sys.stderr) + continue + break + +while True: + os_input = input("Preferred operating system (macOS, Arch Linux, Ubuntu): ").strip() + preferred_os = parse_operating_system(os_input) + if preferred_os is not None: + break + print("Please choose one of: macOS, Arch Linux, Ubuntu.") + + + + +# Create the Person with validated types +person = Person(name=name, age=age, preferred_operating_system=preferred_os) +people.append(person) + +# Find matching laptops and print count +for person in people: + matching_laptops = find_possible_laptops(laptops, person) + print(f"\nHi {person.name}, we found {len(matching_laptops)} laptop(s) acc. your preferred OS ({person.preferred_operating_system.value}).") + +# Count how many laptops exist per OS +mac_count = 0 +arch_count = 0 +ubuntu_count = 0 + +for laptop in laptops: + if laptop.operating_system == OperatingSystem.MACOS: + mac_count += 1 + elif laptop.operating_system == OperatingSystem.ARCH: + arch_count += 1 + elif laptop.operating_system == OperatingSystem.UBUNTU: + ubuntu_count += 1 + +# Find OS with the most laptops +best_os = OperatingSystem.MACOS +best_count = mac_count + +if arch_count > best_count: + best_os = OperatingSystem.ARCH + best_count = arch_count + +if ubuntu_count > best_count: + best_os = OperatingSystem.UBUNTU + best_count = ubuntu_count + +# Compare availability and suggest better OS +preferred_count = 0 +if person.preferred_operating_system == OperatingSystem.MACOS: + preferred_count = mac_count +elif person.preferred_operating_system == OperatingSystem.ARCH: + preferred_count = arch_count +elif person.preferred_operating_system == OperatingSystem.UBUNTU: + preferred_count = ubuntu_count + +if best_os != person.preferred_operating_system and best_count > preferred_count: + print(f"\n More laptops are available if you choose {best_os.value} ({best_count} available).") \ No newline at end of file From 1cd6775cd743a1b43895c01cab01998f2229d9e9 Mon Sep 17 00:00:00 2001 From: sheetalkharab Date: Sun, 12 Oct 2025 17:14:35 +0100 Subject: [PATCH 10/19] gitignore --- .gitignore | 1 + 1 file changed, 1 insertion(+) create mode 100644 .gitignore diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..40b878d --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +node_modules/ \ No newline at end of file From 974852d583d2a33601827cb0802d9e385ff2b9e5 Mon Sep 17 00:00:00 2001 From: sheetalkharab Date: Mon, 13 Oct 2025 12:23:58 +0100 Subject: [PATCH 11/19] update code for type --- bank_account.py | 33 +++++++++++++++++++++++++++++++++ 1 file changed, 33 insertions(+) create mode 100644 bank_account.py diff --git a/bank_account.py b/bank_account.py new file mode 100644 index 0000000..395aa9e --- /dev/null +++ b/bank_account.py @@ -0,0 +1,33 @@ +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, +} +#missed balance argument, add balances +open_account(balances,"Tobi", 913) +#3rd argument is an int not a String and format as pence +open_account(balances,"Olya", 713) + +total_pence = sum_balances(balances) +#wrong name of function +total_string = format_pence_as_string(total_pence) + +print(f"The bank accounts total {total_string}") \ No newline at end of file From c7bb701ec4cc27676cd2c0cec29d072fe2a1faaa Mon Sep 17 00:00:00 2001 From: sheetalkharab Date: Mon, 13 Oct 2025 12:24:32 +0100 Subject: [PATCH 12/19] change file name to follow snake_case --- bankAccount.py | 32 -------------------------------- 1 file changed, 32 deletions(-) delete mode 100644 bankAccount.py diff --git a/bankAccount.py b/bankAccount.py deleted file mode 100644 index eaf5509..0000000 --- a/bankAccount.py +++ /dev/null @@ -1,32 +0,0 @@ -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, -} -#missed balance argument, add balances -open_account(balances,"Tobi", 913) -#3rd argument is an int not a String and format as pence -open_account(balances,"Olya", 713) - -total_pence = sum_balances(balances) -#wrong name of function -total_string = format_pence_as_string(total_pence) - -print(f"The bank accounts total {total_string}") \ No newline at end of file From b337a4043a31118c7834913414f9f6ebfb6e945b Mon Sep 17 00:00:00 2001 From: sheetalkharab Date: Mon, 13 Oct 2025 12:25:07 +0100 Subject: [PATCH 13/19] change file name acc to sanke_case --- dataclass_person.py | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) create mode 100644 dataclass_person.py diff --git a/dataclass_person.py b/dataclass_person.py new file mode 100644 index 0000000..d8327db --- /dev/null +++ b/dataclass_person.py @@ -0,0 +1,21 @@ +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): + today = date.today() + birth_date= self.date_of_birth + #Calculate exact age by subtracting years + eighteen_years_ago = date(today.year - 18, today.month, today.day) + return birth_date<= eighteen_years_ago + +imran = Person("Imran", date(1989,6,25), "Ubuntu") # We can call this constructor - @dataclass generated it for us. +print(imran) # Prints Person(name='Imran', date_of_birth=(1989, 6, 25), preferred_operating_system='Ubuntu') + +imran2 = Person("Imran", date(1989,6,25), "Ubuntu") +print(imran == imran2) # Prints True \ No newline at end of file From 3cf2111f2540bad2a0804cb9168fba05a5c98a5d Mon Sep 17 00:00:00 2001 From: sheetalkharab Date: Mon, 13 Oct 2025 12:25:26 +0100 Subject: [PATCH 14/19] rename file acc to snake_case --- dataclassPerson.py | 21 --------------------- 1 file changed, 21 deletions(-) delete mode 100644 dataclassPerson.py diff --git a/dataclassPerson.py b/dataclassPerson.py deleted file mode 100644 index 524a569..0000000 --- a/dataclassPerson.py +++ /dev/null @@ -1,21 +0,0 @@ -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): - today_date = date.today() - birth_date= self.date_of_birth - #to check date and month as well - age = today_date.year-birth_date.year- ((today_date.month, today_date.day) < (birth_date.month, birth_date.day)) - return age>=18 - -imran = Person("Imran", (1989,6,25), "Ubuntu") # We can call this constructor - @dataclass generated it for us. -print(imran) # Prints Person(name='Imran', date_of_birth=(1989, 6, 25), preferred_operating_system='Ubuntu') - -imran2 = Person("Imran", (1989,6,25), "Ubuntu") -print(imran == imran2) # Prints True \ No newline at end of file From 7ca2702c122750b57ef330e0922b4377decdd513 Mon Sep 17 00:00:00 2001 From: sheetalkharab Date: Mon, 13 Oct 2025 12:25:53 +0100 Subject: [PATCH 15/19] update code logic acc to suggestion --- enums.py | 101 ++++++++++++++++++++++--------------------------------- 1 file changed, 40 insertions(+), 61 deletions(-) diff --git a/enums.py b/enums.py index f49307f..8e71b2f 100644 --- a/enums.py +++ b/enums.py @@ -2,6 +2,7 @@ from enum import Enum import sys from typing import List, Optional +from collections import Counter class OperatingSystem(Enum): MACOS = "macOS" @@ -51,36 +52,38 @@ def parse_operating_system(s: str) -> Optional[OperatingSystem]: Laptop(id=4, manufacturer="Apple", model="macBook", screen_size_in_inches=13, operating_system=OperatingSystem.MACOS), ] +def input_name() -> str: + while True: + name = input("Enter your name: ") + if name == "": + print("Error: Name cannot be empty. Please try again.") + else: + return name + +def input_age() -> int: + while True: + try: + age_input = input("Enter your age: ") + age = int(age_input) + if age < 0: + raise ValueError("Age cannot be negative.") + return age + except ValueError as e: + print(f"Error: {e}", file=sys.stderr) + +def input_operating_system() -> OperatingSystem: + while True: + os_input = input("Preferred operating system (macOS, Arch Linux, Ubuntu): ").strip() + preferred_os = parse_operating_system(os_input) + if preferred_os is not None: + return preferred_os + print("Please choose one of: macOS, Arch Linux, Ubuntu.") + +name = input_name() +age = input_age() +preferred_os = input_operating_system() -while True: - name = input("Enter your name: ") - if name == "": - print("Error: Name cannot be empty. Please try again.") - else: - break - -while True: - age_input = input("Enter your age: ") - if not age_input.isdigit(): - print("Error: Age must be a number.", file=sys.stderr) - continue - age = int(age_input) - if age < 0: - print("Error: Age cannot be negative.", file=sys.stderr) - continue - break - -while True: - os_input = input("Preferred operating system (macOS, Arch Linux, Ubuntu): ").strip() - preferred_os = parse_operating_system(os_input) - if preferred_os is not None: - break - print("Please choose one of: macOS, Arch Linux, Ubuntu.") - - - - # Create the Person with validated types person = Person(name=name, age=age, preferred_operating_system=preferred_os) people.append(person) @@ -90,39 +93,15 @@ def parse_operating_system(s: str) -> Optional[OperatingSystem]: matching_laptops = find_possible_laptops(laptops, person) print(f"\nHi {person.name}, we found {len(matching_laptops)} laptop(s) acc. your preferred OS ({person.preferred_operating_system.value}).") -# Count how many laptops exist per OS -mac_count = 0 -arch_count = 0 -ubuntu_count = 0 - -for laptop in laptops: - if laptop.operating_system == OperatingSystem.MACOS: - mac_count += 1 - elif laptop.operating_system == OperatingSystem.ARCH: - arch_count += 1 - elif laptop.operating_system == OperatingSystem.UBUNTU: - ubuntu_count += 1 - -# Find OS with the most laptops -best_os = OperatingSystem.MACOS -best_count = mac_count - -if arch_count > best_count: - best_os = OperatingSystem.ARCH - best_count = arch_count - -if ubuntu_count > best_count: - best_os = OperatingSystem.UBUNTU - best_count = ubuntu_count - -# Compare availability and suggest better OS -preferred_count = 0 -if person.preferred_operating_system == OperatingSystem.MACOS: - preferred_count = mac_count -elif person.preferred_operating_system == OperatingSystem.ARCH: - preferred_count = arch_count -elif person.preferred_operating_system == OperatingSystem.UBUNTU: - preferred_count = ubuntu_count +# Count how many laptops exist per OS using Counter +os_counts = Counter(laptop.operating_system for laptop in laptops) + +# Find OS with most available laptops +best_os = max(os_counts, key=lambda os: os_counts[os]) +best_count = os_counts[best_os] + +# Compare with user preferences +preferred_count = os_counts.get(person.preferred_operating_system, 0) if best_os != person.preferred_operating_system and best_count > preferred_count: print(f"\n More laptops are available if you choose {best_os.value} ({best_count} available).") \ No newline at end of file From d8cdada98bcbe4ffd5f4d2c206c3480c5dac0634 Mon Sep 17 00:00:00 2001 From: sheetalkharab Date: Mon, 13 Oct 2025 12:26:33 +0100 Subject: [PATCH 16/19] change file name acc to sanke_case --- laptops_allocate.py | 42 ++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 42 insertions(+) create mode 100644 laptops_allocate.py diff --git a/laptops_allocate.py b/laptops_allocate.py new file mode 100644 index 0000000..fa61f05 --- /dev/null +++ b/laptops_allocate.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 == 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 806097cc57dab9508599b599e9c2514e0c1ce878 Mon Sep 17 00:00:00 2001 From: sheetalkharab Date: Mon, 13 Oct 2025 12:26:45 +0100 Subject: [PATCH 17/19] rename file name --- laptopsallocate.py | 42 ------------------------------------------ 1 file changed, 42 deletions(-) delete mode 100644 laptopsallocate.py diff --git a/laptopsallocate.py b/laptopsallocate.py deleted file mode 100644 index fa61f05..0000000 --- a/laptopsallocate.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 = [] - for laptop in laptops: - if laptop.operating_system == 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 0a3bae24b02a6117b8b44fc371de590a6a3f426b Mon Sep 17 00:00:00 2001 From: sheetalkharab Date: Mon, 13 Oct 2025 12:27:07 +0100 Subject: [PATCH 18/19] add mypy in requirement.txt --- requirements.txt | 1 + 1 file changed, 1 insertion(+) create mode 100644 requirements.txt diff --git a/requirements.txt b/requirements.txt new file mode 100644 index 0000000..5ac2d6c --- /dev/null +++ b/requirements.txt @@ -0,0 +1 @@ +mypy==1.18.2 \ No newline at end of file From 49b3dba04faef1ca5f839b35a3a2979e8ef6ce0d Mon Sep 17 00:00:00 2001 From: sheetalkharab Date: Thu, 16 Oct 2025 22:02:09 +0100 Subject: [PATCH 19/19] move function inside class --- dataclass_person.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/dataclass_person.py b/dataclass_person.py index d8327db..a0325b8 100644 --- a/dataclass_person.py +++ b/dataclass_person.py @@ -7,7 +7,7 @@ class Person: date_of_birth: date preferred_operating_system: str -def is_adult(self): + def is_adult(self): today = date.today() birth_date= self.date_of_birth #Calculate exact age by subtracting years