|
| 1 | +from dataclasses import dataclass |
| 2 | +from enum import Enum |
| 3 | +from typing import List |
| 4 | + |
| 5 | +class OperatingSystem(Enum): |
| 6 | + MACOS = "macOS" |
| 7 | + ARCH = "Arch Linux" |
| 8 | + UBUNTU = "Ubuntu" |
| 9 | + WINDOWS = "Windows" |
| 10 | + |
| 11 | +@dataclass(frozen=True) |
| 12 | +class Person: |
| 13 | + name: str |
| 14 | + age: int |
| 15 | + preferred_operating_system: List[OperatingSystem] |
| 16 | + |
| 17 | +@dataclass(frozen=True) |
| 18 | +class Laptop: |
| 19 | + id: int |
| 20 | + manufacturer: str |
| 21 | + model: str |
| 22 | + screen_size_in_inches: float |
| 23 | + operating_system: OperatingSystem |
| 24 | + |
| 25 | +def allocate_laptops(people: List[Person], laptops: List[Laptop]) -> dict[Person, Laptop]: |
| 26 | + allocateLp = {} |
| 27 | + lpNewList = laptops.copy() |
| 28 | + |
| 29 | + if len(laptops) < len(people): |
| 30 | + print("Not enough laptops for all people") |
| 31 | + |
| 32 | + for person in people: |
| 33 | + sadnessNumber = 100 |
| 34 | + |
| 35 | + for lp in lpNewList: |
| 36 | + if lp.operating_system in person.preferred_operating_system: |
| 37 | + sadnessNumber = person.preferred_operating_system.index(lp.operating_system) |
| 38 | + allocateLp[person.name] = (lp.operating_system.value, sadnessNumber) |
| 39 | + lpNewList.remove(lp) |
| 40 | + break |
| 41 | + if sadnessNumber == 100: |
| 42 | + allocateLp[person.name] = ("No Laptop Assigned", sadnessNumber) |
| 43 | + |
| 44 | + return allocateLp |
| 45 | + |
| 46 | + |
| 47 | +personList = [ |
| 48 | + Person(name="Aida", age=30, preferred_operating_system=[OperatingSystem.UBUNTU]), |
| 49 | + Person(name="Elizaveta", age=28, preferred_operating_system=[OperatingSystem.UBUNTU, OperatingSystem.WINDOWS, OperatingSystem.ARCH]), |
| 50 | + Person(name="Zahra", age=35, preferred_operating_system=[OperatingSystem.UBUNTU]), |
| 51 | + Person(name="Tobi", age=35, preferred_operating_system=[OperatingSystem.WINDOWS, OperatingSystem.MACOS, OperatingSystem.ARCH]), |
| 52 | +] |
| 53 | + |
| 54 | +laptopList = [ |
| 55 | + Laptop(id=1, manufacturer="Dell", model="XPS", screen_size_in_inches=13, operating_system=OperatingSystem.ARCH), |
| 56 | + Laptop(id=4, manufacturer="EDell", model="XPS", screen_size_in_inches=13, operating_system=OperatingSystem.ARCH), |
| 57 | + Laptop(id=2, manufacturer="Dell", model="XPS", screen_size_in_inches=15, operating_system=OperatingSystem.UBUNTU), |
| 58 | + Laptop(id=3, manufacturer="Apple", model="macBook", screen_size_in_inches=13, operating_system=OperatingSystem.MACOS), |
| 59 | +] |
| 60 | + |
| 61 | +result = allocate_laptops(personList, laptopList) |
| 62 | +lenResult = len(result) |
| 63 | +for key, value in result.items(): |
| 64 | + print (f" {key } has ** {value[0]} ** and Sadness is: {value[1]}" ) |
| 65 | + |
| 66 | + |
| 67 | + |
0 commit comments