|
| 1 | +from dataclasses import dataclass |
| 2 | +from enum import Enum |
| 3 | +from typing import List,Dict,Tuple |
| 4 | + |
| 5 | + |
| 6 | +class OperatingSystem(Enum): |
| 7 | + MACOS = "macOS" |
| 8 | + ARCH = "Arch Linux" |
| 9 | + UBUNTU = "Ubuntu" |
| 10 | + |
| 11 | +@dataclass(frozen=True) |
| 12 | +class Person: |
| 13 | + name: str |
| 14 | + age: int |
| 15 | + preferred_operating_system: Tuple[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 | +# calculate sadness score |
| 27 | +def calculate_sadness(person:Person,laptop:Laptop) -> int: |
| 28 | + if laptop.operating_system in person.preferred_operating_system: |
| 29 | + sadness=person.preferred_operating_system.index(laptop.operating_system) |
| 30 | + return sadness |
| 31 | + else: |
| 32 | + return 100 |
| 33 | + |
| 34 | +def allocate_laptops(people: List[Person], laptops: List[Laptop]) -> Dict[Person, Laptop]: |
| 35 | + if len(laptops) < len(people): |
| 36 | + raise ValueError("Not enough laptops") |
| 37 | + |
| 38 | + total=float("inf") # store the min sadness |
| 39 | + allocation:Dict[Person, Laptop]={} # store best allocation |
| 40 | + |
| 41 | + # try all possible laptop combinations |
| 42 | + def find_best_allocation(person_index:int,current_total:int,current_allocation: Dict[Person, Laptop],remaining_laptops: List[Laptop]): |
| 43 | + nonlocal total, allocation |
| 44 | + if person_index == len(people): # everybody have a laptop |
| 45 | + |
| 46 | + if current_total<total: # check if this allocation is better |
| 47 | + total=current_total |
| 48 | + allocation=current_allocation.copy() |
| 49 | + return |
| 50 | + |
| 51 | + person=people[person_index] # get current person |
| 52 | + |
| 53 | + for laptop in remaining_laptops: |
| 54 | + sadness=calculate_sadness(person,laptop) |
| 55 | + current_allocation[person]=laptop |
| 56 | + |
| 57 | + find_best_allocation(person_index+1,current_total+sadness,current_allocation,[l for l in remaining_laptops if l !=laptop]) |
| 58 | + del current_allocation[person] |
| 59 | + |
| 60 | + find_best_allocation(0,0,{},laptops) |
| 61 | + |
| 62 | + return allocation |
| 63 | + |
| 64 | +if __name__ == "__main__": |
| 65 | + people = [ |
| 66 | + Person("Imran", 22, (OperatingSystem.UBUNTU,)), |
| 67 | + Person("Eliza", 34, (OperatingSystem.ARCH,)), |
| 68 | + ] |
| 69 | + |
| 70 | + laptops = [ |
| 71 | + Laptop(1, "Dell", "XPS", 13, OperatingSystem.ARCH), |
| 72 | + Laptop(2, "Dell", "XPS", 15, OperatingSystem.UBUNTU), |
| 73 | + Laptop(3, "Dell", "XPS", 15, OperatingSystem.UBUNTU), |
| 74 | + Laptop(4, "Apple", "MacBook", 13, OperatingSystem.MACOS), |
| 75 | + ] |
| 76 | + |
| 77 | + result = allocate_laptops(people, laptops) |
| 78 | + |
| 79 | + print("Allocation result:") |
| 80 | + total = 0 |
| 81 | + for person, laptop in result.items(): |
| 82 | + s = calculate_sadness(person, laptop) |
| 83 | + total += s |
| 84 | + print(f"{person.name} -> {laptop.model} ({laptop.operating_system.value}) | sadness={s}") |
| 85 | + |
| 86 | + print("Total sadness:", total) |
| 87 | + |
0 commit comments