Skip to content

Commit 70c441b

Browse files
Refactor laptop allocation logic to use tuple for preferred operating systems and extract sadness score calculation into a separate function
1 parent cf8959c commit 70c441b

1 file changed

Lines changed: 16 additions & 12 deletions

File tree

implement-laptop-allocation/laptop_allocaton.py

Lines changed: 16 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import sys
22
from dataclasses import dataclass
33
from enum import Enum
4-
from typing import List, Dict
4+
from typing import List, Dict, Tuple
55

66
class OperatingSystem(Enum):
77
"""enumeration of available operating systems."""
@@ -15,7 +15,7 @@ class Person:
1515
name: str
1616
age: int
1717
# listed in order of preference
18-
preferred_operating_systems: List[OperatingSystem]
18+
preferred_operating_systems: Tuple[OperatingSystem, ...]
1919

2020

2121
@dataclass(frozen=True)
@@ -108,17 +108,20 @@ def find_possible_laptops(available_laptops: List[Laptop], current_person: Perso
108108
if laptop.operating_system in current_person.preferred_operating_systems
109109
]
110110

111+
def sadness_score(person: Person, laptop: Laptop) -> int:
112+
"""
113+
calculate the sadness score for a person based on the allocated laptop.
114+
"""
115+
try:
116+
return person.preferred_operating_systems.index(laptop.operating_system)
117+
except ValueError:
118+
return 100
119+
111120

112121
def allocate_laptops(people: List[Person], laptops: List[Laptop]) -> Dict[Person, Laptop]:
113122
"""
114123
allocate laptops to people to minimize total sadness.
115124
"""
116-
def calculate_sadness_score(person: Person, laptop: Laptop) -> int:
117-
try:
118-
return person.preferred_operating_systems.index(laptop.operating_system)
119-
except ValueError:
120-
return 100
121-
122125
allocated_laptops = {}
123126

124127
# create a shallow copy of the laptops list
@@ -129,9 +132,9 @@ def calculate_sadness_score(person: Person, laptop: Laptop) -> int:
129132
if not available_laptops:
130133
raise ValueError("No laptops available to allocate.")
131134

132-
# use min() to select the laptop with the lowest sadness score for the person.
133-
# lambda function is scoring the person's preferences by calculating the "sadness score" for each laptop based on the index position
134-
best_laptop = min(available_laptops, key=lambda laptop: calculate_sadness_score(person, laptop), default=None)
135+
# use min() to find the laptop that minimizes their 'sadness score'
136+
# lambda function is scoring the person's preferences by calculating the 'sadness score' for each laptop based on the index position
137+
best_laptop = min(available_laptops, key=lambda laptop: sadness_score(person, laptop), default=None)
135138
if best_laptop:
136139
allocated_laptops[person.name] = best_laptop
137140
available_laptops.remove(best_laptop)
@@ -151,7 +154,8 @@ def main():
151154
# Allocate laptops and print the results
152155
allocation = allocate_laptops(people, laptops_list)
153156
for name, laptop in allocation.items():
154-
print(f"{name} was allocated {laptop.manufacturer} {laptop.model} with {laptop.operating_system.value}")
157+
person_sadness_score = sadness_score(next(person for person in people if person.name == name), laptop)
158+
print(f"{name} was allocated {laptop.manufacturer} {laptop.model} with {laptop.operating_system.value} (Score: {person_sadness_score})")
155159

156160
# Ensure the script runs only when executed directly
157161
if __name__ == "__main__":

0 commit comments

Comments
 (0)