From 06a22bacc0a2143c9c54ab9eb120c96b1b79b399 Mon Sep 17 00:00:00 2001 From: sarawone Date: Sat, 25 Oct 2025 17:03:51 +0100 Subject: [PATCH] allocation --- allocaton.py | 49 +++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 49 insertions(+) create mode 100644 allocaton.py diff --git a/allocaton.py b/allocaton.py new file mode 100644 index 0000000..eb6ee68 --- /dev/null +++ b/allocaton.py @@ -0,0 +1,49 @@ +from dataclasses import dataclass +from enum import Enum +from typing import List, Dict + +class OperatingSystem(Enum): + MACOS = "macOS" + ARCH = "Arch Linux" + UBUNTU = "Ubuntu" + +@dataclass(frozen=True) +class Person: + name: str + age: int + preferred_operating_system: List[OperatingSystem] + +@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]: + laptops_remaining = laptops.copy() + allocation: Dict[Person, Laptop] = {} + + for person in people: + best_laptop = None + best_sadness = 101 + + + for laptop in laptops_remaining: + if laptop.operating_system in person.preferred_operating_system: + sadness = person.preferred_operating_system.index(laptop.operating_system) + else: + sadness = 100 + + if sadness < best_sadness: + best_sadness = sadness + best_laptop = laptop + + if best_laptop is None: + raise ValueError(f"No laptops available to allocate to {person.name}") + + allocation[person] = best_laptop + laptops_remaining.remove(best_laptop) + + return allocation \ No newline at end of file