Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
node_modules/
88 changes: 88 additions & 0 deletions allocate_laptop.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
from dataclasses import dataclass
from enum import Enum
from typing import Dict, List

class OperatingSystem(Enum):
MACOS = "macOS"
ARCH = "Arch Linux"
UBUNTU = "Ubuntu"

@dataclass(frozen=True)
class Person:
name: str
age: int
# Sorted in order of preference, most preferred is first.
preferred_operating_system: List[OperatingSystem]

def __hash__(self):
return hash((self.name, self.age, tuple(self.preferred_operating_system)))



@dataclass(frozen=True)
class Laptop:
id: int
manufacturer: str
model: str
screen_size_in_inches: float
operating_system: OperatingSystem


def allocate_laptops(people: List[Person], laptops: List[Laptop]) -> Dict[Person, Laptop]:
allocations: Dict[Person, Laptop] = {}
available_laptops = laptops.copy()

for person in people:
# Find laptop that gives this person minimum sadness
if available_laptops:
best_laptop = min(available_laptops, key=lambda l: calculate_sadness(person, l))

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This method works, one thing to be careful of is this think how often this function will run.

It will always run, for every laptop, even if the very first one were a perfect match.

It's not an issue in your example here, but imagine if we were running this with thousands of laptop and people entries

allocations[person] = best_laptop
available_laptops.remove(best_laptop) # Laptop now allocated
else:
# No laptops left, assign None or a “dummy” Laptop with sadness 100
allocations[person] = None

return allocations


def calculate_sadness(person: Person, laptop: Laptop) -> int:
"""Return sadness for a person if given this laptop."""
if laptop is None:
return 100 # no laptop, maximum sadness
try:
return person.preferred_operating_system.index(laptop.operating_system)
except ValueError:
return 100 # OS not in preferences


# Sample laptops
laptops = [
Laptop(id=1, manufacturer="Dell", model="XPS", screen_size_in_inches=13, operating_system=OperatingSystem.ARCH),
Laptop(id=2, manufacturer="Dell", model="XPS", screen_size_in_inches=15, operating_system=OperatingSystem.UBUNTU),
Laptop(id=3, manufacturer="Apple", model="MacBook", screen_size_in_inches=13, operating_system=OperatingSystem.MACOS),
]

# Sample people with preferences
people = [
Person(name="Alice", age=25, preferred_operating_system=[OperatingSystem.UBUNTU, OperatingSystem.MACOS]),
Person(name="Bob", age=30, preferred_operating_system=[OperatingSystem.MACOS, OperatingSystem.ARCH]),
Person(name="Charlie", age=22, preferred_operating_system=[OperatingSystem.ARCH, OperatingSystem.UBUNTU]),
Person(name="Jack", age=18, preferred_operating_system=[OperatingSystem.ARCH, OperatingSystem.UBUNTU]),
Person(name="Faith", age=23, preferred_operating_system=[OperatingSystem.ARCH, OperatingSystem.UBUNTU]),
]

# Allocate laptops
allocations = allocate_laptops(people, laptops)

# Calculate total sadness
total_sadness = sum(calculate_sadness(person, laptop) for person, laptop in allocations.items())
print(f"\nTotal sadness: {total_sadness}")

# Print allocations and sadness
for person, laptop in allocations.items():
if laptop is not None:
print(f"{person.name} got {laptop.manufacturer} {laptop.model} ({laptop.operating_system.value}) "
f"→ Sadness: {calculate_sadness(person, laptop)}")
else:
print(f"{person.name} got no laptop → Sadness: 100")

Loading