From abe33a93efec953a1a25a203fe1a5bd741dbe6e9 Mon Sep 17 00:00:00 2001 From: Droid-An Date: Sat, 20 Sep 2025 12:53:27 +0100 Subject: [PATCH 1/3] wip:publish branch --- .gitignore | 3 ++ laptop_alloctaion.py | 73 ++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 76 insertions(+) create mode 100644 .gitignore create mode 100644 laptop_alloctaion.py diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..da2cbc1 --- /dev/null +++ b/.gitignore @@ -0,0 +1,3 @@ +.DS_Store +**/.DS_Store +.venv \ No newline at end of file diff --git a/laptop_alloctaion.py b/laptop_alloctaion.py new file mode 100644 index 0000000..9b7ea43 --- /dev/null +++ b/laptop_alloctaion.py @@ -0,0 +1,73 @@ +from dataclasses import dataclass +from enum import Enum +from typing import 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=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, + ), +] + + +def allocate_laptops( + people: List[Person], laptops: List[Laptop] +) -> dict[Person, Laptop]: + allocation = {} + laptops_copy = laptops.copy() + for person in people: + for laptop in laptops_copy: + for index in range(len(person.preferred_operating_system)): + if laptop.operating_system == person.preferred_operating_system: + allocation[person] = laptop + # elif + + return allocation From 08c30f9ee52966c1b0b356649975499f829b68d5 Mon Sep 17 00:00:00 2001 From: Droid-An Date: Sun, 21 Sep 2025 14:22:25 +0100 Subject: [PATCH 2/3] done allocation --- laptop_alloctaion.py | 48 ++++++++++++++++++++++++++++++++++++++++---- 1 file changed, 44 insertions(+), 4 deletions(-) diff --git a/laptop_alloctaion.py b/laptop_alloctaion.py index 9b7ea43..18d0df3 100644 --- a/laptop_alloctaion.py +++ b/laptop_alloctaion.py @@ -16,6 +16,9 @@ class Person: # Sorted in order of preference, most preferred is first. preferred_operating_system: List[OperatingSystem] + def __hash__(self): + return hash((self.name, self.age, tuple(self.preferred_operating_system))) + @dataclass(frozen=True) class Laptop: @@ -56,18 +59,55 @@ class Laptop: operating_system=OperatingSystem.MACOS, ), ] +users = [ + Person( + name="Andrei", + age=20, + preferred_operating_system=[OperatingSystem.MACOS, OperatingSystem.UBUNTU], + ), + Person( + name="Eyuel", + age=20, + preferred_operating_system=[ + OperatingSystem.MACOS, + OperatingSystem.UBUNTU, + OperatingSystem.ARCH, + ], + ), + Person( + name="Priscilla", + age=30, + preferred_operating_system=[OperatingSystem.ARCH, OperatingSystem.UBUNTU], + ), + Person( + name="Miki", + age=21, + preferred_operating_system=[OperatingSystem.MACOS, OperatingSystem.UBUNTU], + ), +] + +sadness = 0 def allocate_laptops( people: List[Person], laptops: List[Laptop] ) -> dict[Person, Laptop]: allocation = {} + global sadness laptops_copy = laptops.copy() for person in people: - for laptop in laptops_copy: - for index in range(len(person.preferred_operating_system)): - if laptop.operating_system == person.preferred_operating_system: + for index in range(len(person.preferred_operating_system)): + if person in allocation: + break + for laptop in laptops_copy: + if laptop.operating_system == person.preferred_operating_system[index]: allocation[person] = laptop - # elif + laptops_copy.remove(laptop) + sadness += index + break + elif laptop.operating_system != person.preferred_operating_system[ + index + ] and index + 1 == len(person.preferred_operating_system): + sadness += 100 return allocation From 6df242d76e8ed0b5cfdc8f6f76f3549305b1bb64 Mon Sep 17 00:00:00 2001 From: Droid-An Date: Sun, 21 Sep 2025 15:11:33 +0100 Subject: [PATCH 3/3] implemented forced allocation --- laptop_alloctaion.py | 13 +++++++++---- 1 file changed, 9 insertions(+), 4 deletions(-) diff --git a/laptop_alloctaion.py b/laptop_alloctaion.py index 18d0df3..70cc282 100644 --- a/laptop_alloctaion.py +++ b/laptop_alloctaion.py @@ -105,9 +105,14 @@ def allocate_laptops( laptops_copy.remove(laptop) sadness += index break - elif laptop.operating_system != person.preferred_operating_system[ - index - ] and index + 1 == len(person.preferred_operating_system): - sadness += 100 + # If after distribution person still doesn't have a laptop, give him the first we can find + if person not in allocation: + allocation[person] = laptops[0] + sadness += 100 return allocation + + +allocation = allocate_laptops(users, laptops) +print("\n".join(f"{key} :\n {value}" for key, value in allocation.items())) +print(sadness)