|
| 1 | +from dataclasses import dataclass |
| 2 | +from enum import Enum |
| 3 | +from typing import List, Dict, Tuple, Optional |
| 4 | + |
| 5 | +class OperatingSystem(Enum): |
| 6 | + MACOS = "macOS" |
| 7 | + ARCH = "Arch Linux" |
| 8 | + UBUNTU = "Ubuntu" |
| 9 | + |
| 10 | +@dataclass(frozen=True) |
| 11 | +class Person: |
| 12 | + name: str |
| 13 | + age: int |
| 14 | + # Sorted in order of preference, most preferred is first. |
| 15 | + preferred_operating_systems: List[OperatingSystem] |
| 16 | + |
| 17 | + |
| 18 | +@dataclass(frozen=True) |
| 19 | +class Laptop: |
| 20 | + id: int |
| 21 | + manufacturer: str |
| 22 | + model: str |
| 23 | + screen_size_in_inches: float |
| 24 | + operating_system: OperatingSystem |
| 25 | + |
| 26 | +def norm_os_values(value: str) -> OperatingSystem: |
| 27 | + value = value.strip().lower() |
| 28 | + if value == "ubuntu": |
| 29 | + return OperatingSystem.UBUNTU |
| 30 | + if value == "arch linux": |
| 31 | + return OperatingSystem.ARCH |
| 32 | + if value == "macos": |
| 33 | + return OperatingSystem.MACOS |
| 34 | + raise ValueError(f"Unknown OS: {value}") |
| 35 | + |
| 36 | + |
| 37 | +people = [ |
| 38 | + Person(name="Imran", age=22, preferred_operating_systems=[norm_os_values("Ubuntu"), norm_os_values("Arch Linux")]), |
| 39 | + Person(name="Eliza", age=34, preferred_operating_systems=[norm_os_values("Arch Linux"), norm_os_values("macOS"), norm_os_values("Ubuntu")]), |
| 40 | + Person(name="Ira", age=21, preferred_operating_systems=[norm_os_values("Ubuntu"), norm_os_values("Arch Linux")]), |
| 41 | + Person(name="Anna", age=34, preferred_operating_systems=[norm_os_values("Ubuntu"), norm_os_values("macOS")]), |
| 42 | + Person(name="Nahimn", age=42, preferred_operating_systems=[norm_os_values("Ubuntu"), norm_os_values("Arch Linux")]) |
| 43 | +] |
| 44 | + |
| 45 | +laptops = [ |
| 46 | + Laptop(id=1, manufacturer="Dell", model="XPS", screen_size_in_inches=13, operating_system=norm_os_values("Arch Linux")), |
| 47 | + Laptop(id=2, manufacturer="Dell", model="XPS", screen_size_in_inches=15, operating_system=norm_os_values("Ubuntu")), |
| 48 | + Laptop(id=3, manufacturer="Dell", model="XPS", screen_size_in_inches=15, operating_system=norm_os_values("ubuntu")), |
| 49 | + Laptop(id=4, manufacturer="Apple", model="macBook", screen_size_in_inches=13, operating_system=norm_os_values("macOS")), |
| 50 | +] |
| 51 | + |
| 52 | + |
| 53 | +def allocate_laptops(people: List[Person], laptops: List[Laptop]) -> Dict[Tuple[str, int], int]: |
| 54 | + sadness_table: Dict[Tuple[str, int], int] = {} |
| 55 | + for person in people: |
| 56 | + for laptop in laptops: |
| 57 | + if laptop.operating_system in person.preferred_operating_systems: |
| 58 | + index = person.preferred_operating_systems.index(laptop.operating_system) |
| 59 | + sadness = index |
| 60 | + else: |
| 61 | + sadness = 100 |
| 62 | + sadness_table[(person.name, laptop.id)] = sadness |
| 63 | + return sadness_table |
| 64 | + |
| 65 | + |
| 66 | +sadness_table = allocate_laptops(people, laptops) |
| 67 | + |
| 68 | +allocation_list: List[Tuple[str, Optional[int], int]] = [] |
| 69 | +allocated_laptops: set[int] = set() |
| 70 | +allocated_persons: set[str] = set() |
| 71 | +total_happiness: int = 0 |
| 72 | + |
| 73 | +for (person_name, laptop_id), sadness in sorted(sadness_table.items(), key=lambda value: value[1]): |
| 74 | + if laptop_id in allocated_laptops: |
| 75 | + continue |
| 76 | + if person_name in allocated_persons: |
| 77 | + continue |
| 78 | + allocation_list.append((person_name, laptop_id, sadness)) |
| 79 | + allocated_laptops.add(laptop_id) |
| 80 | + allocated_persons.add(person_name) |
| 81 | + total_happiness += sadness |
| 82 | + print(f"{person_name} got laptop {laptop_id}") |
| 83 | + |
| 84 | + |
| 85 | +for person in people: |
| 86 | + if person.name not in allocated_persons: |
| 87 | + print(f"{person.name} did not get laptop") |
| 88 | +print(f"Total happiness: {total_happiness}") |
| 89 | + |
| 90 | +print(allocation_list) |
| 91 | + |
0 commit comments