1+ #If we define "sadness" as the number of places down in someone's ranking
2+ #the operating system the ended up with (i.e. if your preferences were
3+ #[UBUNTU, ARCH, MACOS] and you were allocated a MACOS machine your sadness would be 2),
4+ # 0 1 2 100
5+ #we want to minimize the total sadness of all people. If we allocate someone a laptop
6+ #with an operating system not in their preferred list, treat them as having a sadness of 100.
7+
8+ from dataclasses import dataclass
9+ from enum import Enum
10+ from typing import List , Dict , Optional
11+
12+
13+ class OperatingSystem (Enum ):
14+ MACOS = "macOS"
15+ ARCH = "Arch Linux"
16+ UBUNTU = "Ubuntu"
17+
18+ @dataclass (frozen = True )
19+ class Person :
20+ name : str
21+ age : int
22+ preferred_operating_system : List [OperatingSystem ]
23+ assigned_laptop : Optional [int ] = None
24+
25+ @dataclass (frozen = True )
26+ class Laptop :
27+ id : int
28+ manufacturer : str
29+ model : str
30+ screen_size_in_inches : float
31+ operating_system : OperatingSystem
32+ assigned : bool = False
33+
34+ def allocate_laptops (people : List [Person ], laptops : List [Laptop ]) -> Dict [str , int ]:
35+ allocation = {}
36+ for person in people :
37+ for os in person .preferred_operating_system :
38+ for laptop in laptops :
39+ if (laptop .operating_system == os and not laptop .assigned ):
40+ allocation [person .name ] = laptop .id
41+ assigned = True
42+ break
43+ if assigned :
44+ break
45+ return allocation
46+
47+ people = [
48+ Person (name = "Imran" , age = 22 , preferred_operating_system = [OperatingSystem .UBUNTU ,OperatingSystem .ARCH ,OperatingSystem .MACOS ]),
49+ Person (name = "Eliza" , age = 34 , preferred_operating_system = [OperatingSystem .ARCH ]),
50+ Person (name = "Maria" , age = 38 , preferred_operating_system = [OperatingSystem .MACOS ,OperatingSystem .ARCH ]),
51+ ]
52+
53+ laptops = [
54+ Laptop (id = 1 , manufacturer = "Dell" , model = "XPS" , screen_size_in_inches = 13 , operating_system = OperatingSystem .ARCH ),
55+ Laptop (id = 2 , manufacturer = "Dell" , model = "XPS" , screen_size_in_inches = 15 , operating_system = OperatingSystem .UBUNTU ),
56+ Laptop (id = 3 , manufacturer = "Dell" , model = "XPS" , screen_size_in_inches = 15 , operating_system = OperatingSystem .UBUNTU ),
57+ Laptop (id = 4 , manufacturer = "Apple" , model = "macBook" , screen_size_in_inches = 13 , operating_system = OperatingSystem .MACOS ),
58+ ]
59+
60+ if __name__ == "__main__" :
61+ allocation = allocate_laptops (people , laptops )
62+ print ("Allocation:" , allocation )
0 commit comments