Skip to content

Commit 3a77f8f

Browse files
committed
laptop-allocation
1 parent 407b010 commit 3a77f8f

File tree

1 file changed

+81
-0
lines changed

1 file changed

+81
-0
lines changed

laptop-allocation.py

Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
from dataclasses import dataclass
2+
from enum import Enum
3+
from typing import List, Dict, Tuple
4+
5+
class OperatingSystem(Enum):
6+
MACOS = "macOS"
7+
ARCH = "Arch Linux"
8+
UBUNTU = "Ubuntu"
9+
10+
@dataclass(frozen=True)
11+
class Person:
12+
name: str
13+
age: int
14+
preferred_operating_system: Tuple[OperatingSystem, ...]
15+
16+
@dataclass(frozen=True)
17+
class Laptop:
18+
id: int
19+
manufacturer: str
20+
model: str
21+
screen_size_in_inches: float
22+
operating_system: OperatingSystem
23+
24+
25+
# Assign each person the best available laptop according to their preference list
26+
# Minimise sadness locally (not globally optimal, but simple and valid for this exercise)
27+
# If no matching OS is available → they get the “least bad” remaining laptop (sadness = 100)
28+
29+
def sadness(person: Person, laptop: Laptop) -> int:
30+
# Return sadness score for assigning this laptop to this person
31+
if laptop.operating_system in person.preferred_operating_system:
32+
return person.preferred_operating_system.index(laptop.operating_system)
33+
return 100
34+
35+
36+
def allocate_laptops(people: List[Person], laptops: List[Laptop]) -> Dict[Person, Laptop]:
37+
if len(people) != len(laptops):
38+
raise ValueError("Number of people must equal number of laptops.")
39+
40+
# Clone list so we can remove laptops as they are assigned
41+
available_laptops = laptops.copy()
42+
43+
# Creating an empty dictionary
44+
# what the dictionary should contain
45+
allocation: Dict[Person, Laptop] = {}
46+
47+
# stores the single Laptop object that gives the lowest sadness for that person
48+
for person in people:
49+
# Pick the laptop with the minimum sadness for this person
50+
best_laptop = min(
51+
available_laptops, # This is a list of laptops that have not been allocated yet
52+
key=lambda laptop: sadness(person, laptop) # calculates how "sad" the person would be if they got that laptop
53+
) # min() returns the laptop with the smallest sadness value.
54+
55+
# assigns that laptop to the person and removes it from the pool of available laptops
56+
allocation[person] = best_laptop
57+
available_laptops.remove(best_laptop)
58+
59+
return allocation
60+
61+
people = [
62+
Person(name="Imran", age=22, preferred_operating_system=(OperatingSystem.UBUNTU, OperatingSystem.ARCH)),
63+
Person(name="Eliza", age=34, preferred_operating_system=(OperatingSystem.ARCH, OperatingSystem.UBUNTU)),
64+
Person(name="Fatma", age=18, preferred_operating_system=(OperatingSystem.MACOS,)),
65+
]
66+
67+
laptops = [
68+
Laptop(id=1, manufacturer="Dell", model="XPS", screen_size_in_inches=13, operating_system=OperatingSystem.ARCH),
69+
Laptop(id=2, manufacturer="Dell", model="XPS", screen_size_in_inches=15, operating_system=OperatingSystem.UBUNTU),
70+
Laptop(id=3, manufacturer="Apple", model="macBook", screen_size_in_inches=13, operating_system=OperatingSystem.UBUNTU),
71+
]
72+
73+
74+
if __name__ == "__main__":
75+
allocation = allocate_laptops(people, laptops)
76+
77+
print("\nLaptop Allocation Results:\n")
78+
for person, laptop in allocation.items():
79+
sad = sadness(person, laptop)
80+
print(f"{person.name} → Laptop {laptop.id} ({laptop.operating_system.value}) | Sadness = {sad}")
81+

0 commit comments

Comments
 (0)