-
-
Notifications
You must be signed in to change notification settings - Fork 31
Expand file tree
/
Copy pathimplement_laptop_all.py
More file actions
95 lines (73 loc) · 2.63 KB
/
implement_laptop_all.py
File metadata and controls
95 lines (73 loc) · 2.63 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
from dataclasses import dataclass
from enum import Enum
from typing import List, Dict
# Define possible operating systems
class OperatingSystem(Enum):
MACOS = "macOS"
ARCH = "Arch Linux"
UBUNTU = "Ubuntu"
# Define Person and Laptop
@dataclass
class Person:
name: str
age: int
preferred_operating_system: List[OperatingSystem]
@dataclass
class Laptop:
id: int
manufacturer: str
model: str
operating_system: OperatingSystem
# Function to calculate sadness
def sadness_for_person(person: Person, laptop: Laptop) -> int:
"""How sad a person is with this laptop."""
if laptop.operating_system in person.preferred_operating_system:
return person.preferred_operating_system.index(laptop.operating_system)
else:
return 100 # Very sad if OS not in their list
# Simple laptop allocation
def allocate_laptops(people: List[Person], laptops: List[Laptop]) -> Dict[str, Laptop]:
"""
Give each person one laptop.
Try to reduce sadness.
"""
allocated = {}
available_laptops = laptops.copy()
for person in people:
best_laptop = None
best_sadness = 999
# Try to find the best laptop for this person
for laptop in available_laptops:
s = sadness_for_person(person, laptop)
if s < best_sadness:
best_sadness = s
best_laptop = laptop
# Give them the best one we found
allocated[person.name] = best_laptop
available_laptops.remove(best_laptop)
return allocated
# Testing data
def main():
laptops = [
Laptop(1, "Dell", "XPS 13", OperatingSystem.ARCH),
Laptop(2, "Dell", "XPS 15", OperatingSystem.UBUNTU),
Laptop(3, "Apple", "MacBook Air", OperatingSystem.MACOS),
Laptop(4, "Dell", "Inspiron", OperatingSystem.UBUNTU),
]
people = [
Person("Imran", 22, [OperatingSystem.UBUNTU, OperatingSystem.ARCH, OperatingSystem.MACOS]),
Person("Eliza", 34, [OperatingSystem.ARCH, OperatingSystem.UBUNTU, OperatingSystem.MACOS]),
Person("Sam", 29, [OperatingSystem.MACOS, OperatingSystem.UBUNTU]),
Person("Tariq", 25, [OperatingSystem.ARCH]),
]
allocations = allocate_laptops(people, laptops)
print("=== Laptop Allocations ===")
total_sadness = 0
for name, laptop in allocations.items():
person = next(p for p in people if p.name == name)
sad = sadness_for_person(person, laptop)
total_sadness += sad
print(f"{name} gets {laptop.model} ({laptop.operating_system.value}) - sadness {sad}")
print(f"\nTotal sadness: {total_sadness}")
if __name__ == "__main__":
main()