From f4bb681c8076e9156fa912b994beb84086ff807e Mon Sep 17 00:00:00 2001 From: sheida-shab Date: Wed, 3 Dec 2025 22:45:29 +0000 Subject: [PATCH 01/11] define people and laptops data & write basic code to allocate laptops --- .gitignore | 1 + laptop_allocation.py | 50 ++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 51 insertions(+) create mode 100644 laptop_allocation.py diff --git a/.gitignore b/.gitignore index 3c3629e6..f3b72dd5 100644 --- a/.gitignore +++ b/.gitignore @@ -1 +1,2 @@ node_modules +implement-cowsay/.venv \ No newline at end of file diff --git a/laptop_allocation.py b/laptop_allocation.py new file mode 100644 index 00000000..ac2c41ba --- /dev/null +++ b/laptop_allocation.py @@ -0,0 +1,50 @@ +from dataclasses import dataclass +from enum import Enum +from typing import Dict, List + +class OperatingSystem(Enum): + MACOS = "macOS" + ARCH = "Arch Linux" + UBUNTU = "Ubuntu" + +@dataclass(frozen=True) +class Person: + name: str + age: int + # Sorted in order of preference, most preferred is first. + preferred_operating_system: List[OperatingSystem] + + +@dataclass(frozen=True) +class Laptop: + id: int + manufacturer: str + model: str + screen_size_in_inches: float + operating_system: OperatingSystem + +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"), +] +people = [ + Person(name="Imran", age=22, preferred_operating_system=["Arch Linux","Ubuntu"]), + Person(name="Eliza", age=34, preferred_operating_system=["Arch Linux","macOS","Ubuntu"]), + Person(name="Leila", age=45, preferred_operating_system=["macOS","Ubuntu","Arch Linux",]), + Person(name="Mary", age=35, preferred_operating_system=["macOS","Arch Linux"]), +] +def allocate_laptops(people: List[Person], laptops: List[Laptop]) -> Dict[Person, Laptop]: + for person in people : + allocated=False + for i in range(len(person.preferred_operating_system)) : + for laptop in laptops : + if person.preferred_operating_system[i] == laptop.operating_system : + print(person.name,laptop.id,laptop.operating_system,i) + laptops.remove(laptop) + allocated=True + break + if allocated : break + +allocate_laptops(people,laptops) From 1fe0e49f162a984b9288fa88fb4ae721338dbcc8 Mon Sep 17 00:00:00 2001 From: sheida-shab Date: Thu, 4 Dec 2025 12:45:05 +0000 Subject: [PATCH 02/11] improving codeand assign laptops to every person --- laptop_allocation.py | 40 +++++++++++++++++++++++++++------------- 1 file changed, 27 insertions(+), 13 deletions(-) diff --git a/laptop_allocation.py b/laptop_allocation.py index ac2c41ba..edb7b1d9 100644 --- a/laptop_allocation.py +++ b/laptop_allocation.py @@ -24,27 +24,41 @@ class Laptop: operating_system: OperatingSystem 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"), + 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), ] people = [ - Person(name="Imran", age=22, preferred_operating_system=["Arch Linux","Ubuntu"]), - Person(name="Eliza", age=34, preferred_operating_system=["Arch Linux","macOS","Ubuntu"]), - Person(name="Leila", age=45, preferred_operating_system=["macOS","Ubuntu","Arch Linux",]), - Person(name="Mary", age=35, preferred_operating_system=["macOS","Arch Linux"]), + Person(name="Imran", age=22, preferred_operating_system=[OperatingSystem.ARCH,OperatingSystem.UBUNTU]), + Person(name="Eliza", age=34, preferred_operating_system=[OperatingSystem.ARCH,OperatingSystem.MACOS,OperatingSystem.UBUNTU]), + Person(name="Leila", age=45, preferred_operating_system=[OperatingSystem.MACOS,OperatingSystem.UBUNTU,OperatingSystem.ARCH]), + Person(name="Mary", age=35, preferred_operating_system=[OperatingSystem.MACOS,OperatingSystem.ARCH]), ] def allocate_laptops(people: List[Person], laptops: List[Laptop]) -> Dict[Person, Laptop]: + allocated_history : Dict[Person,Laptop] ={} + sadness=0 for person in people : - allocated=False + allocated_flag=False for i in range(len(person.preferred_operating_system)) : for laptop in laptops : if person.preferred_operating_system[i] == laptop.operating_system : - print(person.name,laptop.id,laptop.operating_system,i) + allocated_history[person.name]=laptop + # print(person.name,laptop.id,laptop.operating_system,i) + sadness += i laptops.remove(laptop) - allocated=True + allocated_flag=True break - if allocated : break + if allocated_flag : + break + + if not allocated_flag : + allocated_history[person.name]=laptops[0] + laptops.remove(laptops[0]) + sadness +=100 + + + + return allocated_history ,sadness -allocate_laptops(people,laptops) +print(allocate_laptops(people,laptops)) From 6ebb6fea090fa06c9249bca58b2d9024cad71a4b Mon Sep 17 00:00:00 2001 From: sheida-shab Date: Sun, 7 Dec 2025 15:37:37 +0000 Subject: [PATCH 03/11] refactoring code & fixing bugs --- laptop_allocation.py | 21 +++++++++++++-------- 1 file changed, 13 insertions(+), 8 deletions(-) diff --git a/laptop_allocation.py b/laptop_allocation.py index edb7b1d9..f0a29c8b 100644 --- a/laptop_allocation.py +++ b/laptop_allocation.py @@ -35,10 +35,12 @@ class Laptop: Person(name="Leila", age=45, preferred_operating_system=[OperatingSystem.MACOS,OperatingSystem.UBUNTU,OperatingSystem.ARCH]), Person(name="Mary", age=35, preferred_operating_system=[OperatingSystem.MACOS,OperatingSystem.ARCH]), ] +sadness=0 def allocate_laptops(people: List[Person], laptops: List[Laptop]) -> Dict[Person, Laptop]: + sorted_people_OS_count=sorted(people,key=lambda p:len(p.preferred_operating_system)) allocated_history : Dict[Person,Laptop] ={} - sadness=0 - for person in people : + global sadness + for person in sorted_people_OS_count : allocated_flag=False for i in range(len(person.preferred_operating_system)) : for laptop in laptops : @@ -56,9 +58,12 @@ def allocate_laptops(people: List[Person], laptops: List[Laptop]) -> Dict[Person allocated_history[person.name]=laptops[0] laptops.remove(laptops[0]) sadness +=100 - - - - return allocated_history ,sadness - -print(allocate_laptops(people,laptops)) + + return allocated_history + +def print_final_allocation(allocated_history:dict[Person,Laptop]) : + for name , laptop in allocated_history.items() : + print(f"{name:<10} : Laptop Id {laptop.id:<3} - OS({laptop.operating_system}) ") + print(f"Total sadness is : {sadness}") + +print_final_allocation(allocate_laptops(people,laptops)) \ No newline at end of file From 84515a1576f5fd0c1ec9c46cfdbce951ff726f4e Mon Sep 17 00:00:00 2001 From: sheida-shab Date: Sun, 7 Dec 2025 15:41:00 +0000 Subject: [PATCH 04/11] added some data --- laptop_allocation.py | 3 +++ 1 file changed, 3 insertions(+) diff --git a/laptop_allocation.py b/laptop_allocation.py index f0a29c8b..05f0247e 100644 --- a/laptop_allocation.py +++ b/laptop_allocation.py @@ -28,12 +28,15 @@ class Laptop: 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), + Laptop(id=5, manufacturer="Apple", model="macBook", screen_size_in_inches=13, operating_system=OperatingSystem.MACOS), + Laptop(id=6, manufacturer="Apple", model="macBook", screen_size_in_inches=13, operating_system=OperatingSystem.ARCH), ] people = [ Person(name="Imran", age=22, preferred_operating_system=[OperatingSystem.ARCH,OperatingSystem.UBUNTU]), Person(name="Eliza", age=34, preferred_operating_system=[OperatingSystem.ARCH,OperatingSystem.MACOS,OperatingSystem.UBUNTU]), Person(name="Leila", age=45, preferred_operating_system=[OperatingSystem.MACOS,OperatingSystem.UBUNTU,OperatingSystem.ARCH]), Person(name="Mary", age=35, preferred_operating_system=[OperatingSystem.MACOS,OperatingSystem.ARCH]), + Person(name="Sara", age=28, preferred_operating_system=[OperatingSystem.MACOS]) ] sadness=0 def allocate_laptops(people: List[Person], laptops: List[Laptop]) -> Dict[Person, Laptop]: From 86a77ddfc4d633c7664f94a7a6b03ccc6337adee Mon Sep 17 00:00:00 2001 From: sheida-shab Date: Sun, 7 Dec 2025 16:16:15 +0000 Subject: [PATCH 05/11] Added a few comments --- laptop_allocation.py | 18 +++++++++++++----- 1 file changed, 13 insertions(+), 5 deletions(-) diff --git a/laptop_allocation.py b/laptop_allocation.py index 05f0247e..81c049d8 100644 --- a/laptop_allocation.py +++ b/laptop_allocation.py @@ -2,11 +2,13 @@ from enum import Enum from typing import Dict, List +# Define available operating systems class OperatingSystem(Enum): MACOS = "macOS" ARCH = "Arch Linux" UBUNTU = "Ubuntu" + @dataclass(frozen=True) class Person: name: str @@ -23,6 +25,7 @@ class Laptop: screen_size_in_inches: float operating_system: OperatingSystem +# List of available laptops 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), @@ -31,6 +34,8 @@ class Laptop: Laptop(id=5, manufacturer="Apple", model="macBook", screen_size_in_inches=13, operating_system=OperatingSystem.MACOS), Laptop(id=6, manufacturer="Apple", model="macBook", screen_size_in_inches=13, operating_system=OperatingSystem.ARCH), ] + +# List of people to allocate laptops to people = [ Person(name="Imran", age=22, preferred_operating_system=[OperatingSystem.ARCH,OperatingSystem.UBUNTU]), Person(name="Eliza", age=34, preferred_operating_system=[OperatingSystem.ARCH,OperatingSystem.MACOS,OperatingSystem.UBUNTU]), @@ -38,7 +43,11 @@ class Laptop: Person(name="Mary", age=35, preferred_operating_system=[OperatingSystem.MACOS,OperatingSystem.ARCH]), Person(name="Sara", age=28, preferred_operating_system=[OperatingSystem.MACOS]) ] + +# Global sadness counter sadness=0 + +# Allocate laptops to people to minimize total sadness def allocate_laptops(people: List[Person], laptops: List[Laptop]) -> Dict[Person, Laptop]: sorted_people_OS_count=sorted(people,key=lambda p:len(p.preferred_operating_system)) allocated_history : Dict[Person,Laptop] ={} @@ -48,19 +57,18 @@ def allocate_laptops(people: List[Person], laptops: List[Laptop]) -> Dict[Person for i in range(len(person.preferred_operating_system)) : for laptop in laptops : if person.preferred_operating_system[i] == laptop.operating_system : - allocated_history[person.name]=laptop - # print(person.name,laptop.id,laptop.operating_system,i) - sadness += i + allocated_history[person.name]=laptop # assign laptop + sadness += i # increment sadness by preference index laptops.remove(laptop) allocated_flag=True break if allocated_flag : break - if not allocated_flag : + if not allocated_flag : # assign any remaining laptop if preferred OS not found allocated_history[person.name]=laptops[0] laptops.remove(laptops[0]) - sadness +=100 + sadness +=100 # high sadness for non-preferred OS return allocated_history From aa584cc5270dd0f76268ef0026dee1899ea5e77a Mon Sep 17 00:00:00 2001 From: sheida-shab Date: Sun, 7 Dec 2025 16:22:23 +0000 Subject: [PATCH 06/11] Fixeing laptop os name in print --- laptop_allocation.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/laptop_allocation.py b/laptop_allocation.py index 81c049d8..6ffde7f6 100644 --- a/laptop_allocation.py +++ b/laptop_allocation.py @@ -74,7 +74,7 @@ def allocate_laptops(people: List[Person], laptops: List[Laptop]) -> Dict[Person def print_final_allocation(allocated_history:dict[Person,Laptop]) : for name , laptop in allocated_history.items() : - print(f"{name:<10} : Laptop Id {laptop.id:<3} - OS({laptop.operating_system}) ") + print(f"{name:<10} : Laptop Id {laptop.id:<3} - OS({laptop.operating_system.name}) ") print(f"Total sadness is : {sadness}") print_final_allocation(allocate_laptops(people,laptops)) \ No newline at end of file From 6369bda254a6411a9807d04c9e8a63d8925d0aba Mon Sep 17 00:00:00 2001 From: sheida-shab Date: Wed, 7 Jan 2026 12:04:34 +0000 Subject: [PATCH 07/11] Fixing the global variable issue --- laptop_allocation.py | 13 +++++++------ 1 file changed, 7 insertions(+), 6 deletions(-) diff --git a/laptop_allocation.py b/laptop_allocation.py index 6ffde7f6..690c1912 100644 --- a/laptop_allocation.py +++ b/laptop_allocation.py @@ -44,14 +44,13 @@ class Laptop: Person(name="Sara", age=28, preferred_operating_system=[OperatingSystem.MACOS]) ] -# Global sadness counter -sadness=0 # Allocate laptops to people to minimize total sadness def allocate_laptops(people: List[Person], laptops: List[Laptop]) -> Dict[Person, Laptop]: sorted_people_OS_count=sorted(people,key=lambda p:len(p.preferred_operating_system)) + sadness=0 # local variable sadness counter allocated_history : Dict[Person,Laptop] ={} - global sadness + for person in sorted_people_OS_count : allocated_flag=False for i in range(len(person.preferred_operating_system)) : @@ -70,11 +69,13 @@ def allocate_laptops(people: List[Person], laptops: List[Laptop]) -> Dict[Person laptops.remove(laptops[0]) sadness +=100 # high sadness for non-preferred OS - return allocated_history + return allocated_history , sadness -def print_final_allocation(allocated_history:dict[Person,Laptop]) : +def print_final_allocation(allocated_history :dict[Person,Laptop], sadness:int ) : for name , laptop in allocated_history.items() : print(f"{name:<10} : Laptop Id {laptop.id:<3} - OS({laptop.operating_system.name}) ") print(f"Total sadness is : {sadness}") -print_final_allocation(allocate_laptops(people,laptops)) \ No newline at end of file +allocated_history, sadness = allocate_laptops(people, laptops) + +print_final_allocation(allocated_history, sadness) \ No newline at end of file From 95e0e1dda4a3baa5c25115a118e96b33b85b89e3 Mon Sep 17 00:00:00 2001 From: sheida-shab Date: Wed, 7 Jan 2026 12:13:03 +0000 Subject: [PATCH 08/11] Adding comment about modifying laptop list --- laptop_allocation.py | 3 +++ 1 file changed, 3 insertions(+) diff --git a/laptop_allocation.py b/laptop_allocation.py index 690c1912..e1c9b123 100644 --- a/laptop_allocation.py +++ b/laptop_allocation.py @@ -46,6 +46,9 @@ class Laptop: # Allocate laptops to people to minimize total sadness +# NOTE: laptops list is modified on purpose. +# After allocation, it shows the laptops that are still left. + def allocate_laptops(people: List[Person], laptops: List[Laptop]) -> Dict[Person, Laptop]: sorted_people_OS_count=sorted(people,key=lambda p:len(p.preferred_operating_system)) sadness=0 # local variable sadness counter From fa7880c35371107c81c03f53d4d334e8fa43ea89 Mon Sep 17 00:00:00 2001 From: sheida-shab Date: Wed, 7 Jan 2026 12:20:14 +0000 Subject: [PATCH 09/11] Fix dictionary key type to use person.name --- laptop_allocation.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/laptop_allocation.py b/laptop_allocation.py index e1c9b123..0a8cd688 100644 --- a/laptop_allocation.py +++ b/laptop_allocation.py @@ -49,7 +49,7 @@ class Laptop: # NOTE: laptops list is modified on purpose. # After allocation, it shows the laptops that are still left. -def allocate_laptops(people: List[Person], laptops: List[Laptop]) -> Dict[Person, Laptop]: +def allocate_laptops(people: List[Person], laptops: List[Laptop]) -> Dict[str, Laptop]: sorted_people_OS_count=sorted(people,key=lambda p:len(p.preferred_operating_system)) sadness=0 # local variable sadness counter allocated_history : Dict[Person,Laptop] ={} @@ -74,7 +74,7 @@ def allocate_laptops(people: List[Person], laptops: List[Laptop]) -> Dict[Person return allocated_history , sadness -def print_final_allocation(allocated_history :dict[Person,Laptop], sadness:int ) : +def print_final_allocation(allocated_history :dict[str,Laptop], sadness:int ) : for name , laptop in allocated_history.items() : print(f"{name:<10} : Laptop Id {laptop.id:<3} - OS({laptop.operating_system.name}) ") print(f"Total sadness is : {sadness}") From 1771af38a1e37e06fca0434f2f2f033a6d32a749 Mon Sep 17 00:00:00 2001 From: sheida-shab Date: Wed, 7 Jan 2026 12:30:29 +0000 Subject: [PATCH 10/11] Add a comment in .gitignore file --- .gitignore | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/.gitignore b/.gitignore index f3b72dd5..02025e92 100644 --- a/.gitignore +++ b/.gitignore @@ -1,2 +1,4 @@ node_modules -implement-cowsay/.venv \ No newline at end of file +# Old cowsay virtual environment, intentionally kept +implement-cowsay/.venv + From 07583fd193f698eb0a99ef15536bba4cf3751880 Mon Sep 17 00:00:00 2001 From: sheida-shab Date: Wed, 14 Jan 2026 08:03:22 +0000 Subject: [PATCH 11/11] Rename variable sadness to total_sadness for more readability --- laptop_allocation.py | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/laptop_allocation.py b/laptop_allocation.py index 0a8cd688..b1df3d17 100644 --- a/laptop_allocation.py +++ b/laptop_allocation.py @@ -51,7 +51,7 @@ class Laptop: def allocate_laptops(people: List[Person], laptops: List[Laptop]) -> Dict[str, Laptop]: sorted_people_OS_count=sorted(people,key=lambda p:len(p.preferred_operating_system)) - sadness=0 # local variable sadness counter + total_sadness=0 # local variable sadness summer allocated_history : Dict[Person,Laptop] ={} for person in sorted_people_OS_count : @@ -60,7 +60,7 @@ def allocate_laptops(people: List[Person], laptops: List[Laptop]) -> Dict[str, L for laptop in laptops : if person.preferred_operating_system[i] == laptop.operating_system : allocated_history[person.name]=laptop # assign laptop - sadness += i # increment sadness by preference index + total_sadness += i # increment sadness by preference index laptops.remove(laptop) allocated_flag=True break @@ -70,9 +70,9 @@ def allocate_laptops(people: List[Person], laptops: List[Laptop]) -> Dict[str, L if not allocated_flag : # assign any remaining laptop if preferred OS not found allocated_history[person.name]=laptops[0] laptops.remove(laptops[0]) - sadness +=100 # high sadness for non-preferred OS + total_sadness +=100 # high sadness for non-preferred OS - return allocated_history , sadness + return allocated_history , total_sadness def print_final_allocation(allocated_history :dict[str,Laptop], sadness:int ) : for name , laptop in allocated_history.items() :