Skip to content

Commit 333ea48

Browse files
Add find_preferred_laptops and calculate_sadness helper functions
1 parent ffdcea1 commit 333ea48

1 file changed

Lines changed: 89 additions & 1 deletion

File tree

laptop_allocation/laptop_allocation.py

Lines changed: 89 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,19 @@
11
from dataclasses import dataclass
22
from enum import Enum
33
from typing import List
4+
from typing import Dict
45

6+
# ============================================================================
7+
# ENUM DEFINITIONS
8+
# ============================================================================
59
class OperatingSystem(Enum):
610
MACOS = "macOS"
711
ARCH = "Arch Linux"
812
UBUNTU = "Ubuntu"
913

14+
# ============================================================================
15+
# DATA CLASSES
16+
# ============================================================================
1017
@dataclass(frozen=True)
1118
class Person:
1219
name: str
@@ -21,4 +28,85 @@ class Laptop:
2128
manufacturer: str
2229
model: str
2330
screen_size_in_inches: float
24-
operating_system: OperatingSystem
31+
operating_system: OperatingSystem
32+
33+
# ============================================================================
34+
# HELPER FUNCTIONS
35+
# ============================================================================
36+
"""
37+
Calculates how "sad" a person would be with a given laptop allocation.
38+
39+
Sadness is defined as the position (0-indexed) of the laptop's OS
40+
in the person's preference list. For example:
41+
- If preferences are [UBUNTU, ARCH, MACOS] and they get UBUNTU: sadness = 0
42+
- If preferences are [UBUNTU, ARCH, MACOS] and they get ARCH: sadness = 1
43+
- If preferences are [UBUNTU, ARCH, MACOS] and they get MACOS: sadness = 2
44+
- If they get an OS not in their preferences: sadness = 100
45+
46+
Args:
47+
person: The person receiving the laptop
48+
laptop: The laptop being allocated
49+
50+
Returns:
51+
An integer representing sadness (0 = most happy, 100 = not in preferences)
52+
"""
53+
def find_preferred_laptops(person:Person, laptops: List[Laptop]) -> list[Laptop]:
54+
preferred_laptops = []
55+
for laptop in laptops:
56+
if laptop.operating_system in person.preferred_operating_system:
57+
preferred_laptops.append(laptop)
58+
return preferred_laptops
59+
60+
def calculate_sadness(person:Person, laptop:Laptop)-> int:
61+
if laptop.operating_system in person.preferred_operating_system:
62+
# Find the index (position) of this OS in their preference list
63+
sadness = person.preferred_operating_system.index(laptop.operating_system)
64+
return sadness
65+
else:
66+
# OS not in preferences = very sad
67+
return 100
68+
69+
# ============================================================================
70+
# TEST DATA
71+
# ============================================================================
72+
73+
laptops = [
74+
Laptop(id=1, manufacturer="Dell", model="XPS", screen_size_in_inches=13,
75+
operating_system=OperatingSystem.ARCH),
76+
Laptop(id=2, manufacturer="Dell", model="XPS", screen_size_in_inches=15,
77+
operating_system=OperatingSystem.UBUNTU),
78+
Laptop(id=3, manufacturer="Dell", model="XPS", screen_size_in_inches=15,
79+
operating_system=OperatingSystem.UBUNTU),
80+
Laptop(id=4, manufacturer="Apple", model="macBook", screen_size_in_inches=13,
81+
operating_system=OperatingSystem.MACOS),
82+
Laptop(id=5, manufacturer="Apple", model="macBook Pro", screen_size_in_inches=16,
83+
operating_system=OperatingSystem.MACOS),
84+
Laptop(id=6, manufacturer="Lenovo", model="ThinkPad", screen_size_in_inches=14,
85+
operating_system=OperatingSystem.UBUNTU),
86+
Laptop(id=7, manufacturer="System76", model="Lemur Pro", screen_size_in_inches=15,
87+
operating_system=OperatingSystem.ARCH),
88+
Laptop(id=8, manufacturer="Framework", model="Framework 13", screen_size_in_inches=13,
89+
operating_system=OperatingSystem.UBUNTU),
90+
]
91+
92+
people = [
93+
Person(name="Imran", age=22, preferred_operating_system=[OperatingSystem.UBUNTU, OperatingSystem.MACOS]),
94+
Person(name="Eliza", age=34, preferred_operating_system=[OperatingSystem.ARCH, OperatingSystem.UBUNTU]),
95+
Person(name="Marcus", age=28, preferred_operating_system=[OperatingSystem.MACOS, OperatingSystem.UBUNTU]),
96+
Person(name="Sofia", age=31, preferred_operating_system=[OperatingSystem.UBUNTU]),
97+
Person(name="James", age=25, preferred_operating_system=[OperatingSystem.ARCH, OperatingSystem.MACOS]),
98+
Person(name="Nina", age=29, preferred_operating_system=[OperatingSystem.MACOS, OperatingSystem.ARCH, OperatingSystem.UBUNTU]),
99+
]
100+
101+
102+
103+
# ============================================================================
104+
# TESTING
105+
# ============================================================================
106+
# Test the helper functions
107+
for person in people:
108+
print(f"\n{person.name}'s preferences: {[os.value for os in person.preferred_operating_system]}")
109+
for laptop in laptops:
110+
sadness= calculate_sadness(person, laptop)
111+
print(f"Laptop {laptop.id} ({laptop.operating_system.value}): sadness = {sadness}")
112+

0 commit comments

Comments
 (0)