Skip to content

Commit 06a22ba

Browse files
committed
allocation
1 parent 16ace89 commit 06a22ba

File tree

1 file changed

+49
-0
lines changed

1 file changed

+49
-0
lines changed

allocaton.py

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
from dataclasses import dataclass
2+
from enum import Enum
3+
from typing import List, Dict
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: List[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+
def allocate_laptops(people: List[Person], laptops: List[Laptop]) -> Dict[Person, Laptop]:
25+
laptops_remaining = laptops.copy()
26+
allocation: Dict[Person, Laptop] = {}
27+
28+
for person in people:
29+
best_laptop = None
30+
best_sadness = 101
31+
32+
33+
for laptop in laptops_remaining:
34+
if laptop.operating_system in person.preferred_operating_system:
35+
sadness = person.preferred_operating_system.index(laptop.operating_system)
36+
else:
37+
sadness = 100
38+
39+
if sadness < best_sadness:
40+
best_sadness = sadness
41+
best_laptop = laptop
42+
43+
if best_laptop is None:
44+
raise ValueError(f"No laptops available to allocate to {person.name}")
45+
46+
allocation[person] = best_laptop
47+
laptops_remaining.remove(best_laptop)
48+
49+
return allocation

0 commit comments

Comments
 (0)