|
| 1 | +from enum import Enum |
| 2 | +from typing import List |
| 3 | +from dataclasses import dataclass |
| 4 | + |
| 5 | + |
| 6 | +class OperatingSystem(Enum): |
| 7 | + MACOS = "macOS" |
| 8 | + ARCH = "Arch Linux" |
| 9 | + UBUNTU = "Ubuntu" |
| 10 | + |
| 11 | + |
| 12 | +@dataclass(frozen=True) |
| 13 | +class Laptop: |
| 14 | + id: int |
| 15 | + manufacturer: str |
| 16 | + model: str |
| 17 | + screen_size_in_inches: int |
| 18 | + operating_system: OperatingSystem |
| 19 | + |
| 20 | + |
| 21 | +@dataclass(frozen=True) |
| 22 | +class Person: |
| 23 | + name: str |
| 24 | + age: int |
| 25 | + preferred_operating_system: OperatingSystem |
| 26 | + |
| 27 | + |
| 28 | +laptops = [ |
| 29 | + Laptop( |
| 30 | + id=1, |
| 31 | + manufacturer="Dell", |
| 32 | + model="XPS", |
| 33 | + screen_size_in_inches=13, |
| 34 | + operating_system=OperatingSystem.ARCH, |
| 35 | + ), |
| 36 | + Laptop( |
| 37 | + id=2, |
| 38 | + manufacturer="Dell", |
| 39 | + model="XPS", |
| 40 | + screen_size_in_inches=15, |
| 41 | + operating_system=OperatingSystem.UBUNTU, |
| 42 | + ), |
| 43 | + Laptop( |
| 44 | + id=3, |
| 45 | + manufacturer="Dell", |
| 46 | + model="XPS", |
| 47 | + screen_size_in_inches=15, |
| 48 | + operating_system=OperatingSystem.UBUNTU, |
| 49 | + ), |
| 50 | + Laptop( |
| 51 | + id=4, |
| 52 | + manufacturer="Apple", |
| 53 | + model="macBook", |
| 54 | + screen_size_in_inches=13, |
| 55 | + operating_system=OperatingSystem.MACOS, |
| 56 | + ), |
| 57 | +] |
| 58 | + |
| 59 | + |
| 60 | +def group_laptops_by_operating_system( |
| 61 | + laptops: List[Laptop], |
| 62 | +) -> dict[OperatingSystem, List[Laptop]]: |
| 63 | + available_laptops: dict[OperatingSystem, List[Laptop]] = { |
| 64 | + OperatingSystem.UBUNTU: [], |
| 65 | + OperatingSystem.ARCH: [], |
| 66 | + OperatingSystem.MACOS: [], |
| 67 | + } |
| 68 | + for laptop in laptops: |
| 69 | + available_laptops[laptop.operating_system].append(laptop) |
| 70 | + |
| 71 | + return available_laptops |
| 72 | + |
| 73 | + |
| 74 | +def how_many_match( |
| 75 | + person: Person, available_laptops: dict[OperatingSystem, List[Laptop]] |
| 76 | +) -> int: |
| 77 | + return len(available_laptops[person.preferred_operating_system]) |
| 78 | + |
| 79 | + |
| 80 | +def most_available_operating_system( |
| 81 | + available_laptops: dict[OperatingSystem, List[Laptop]], |
| 82 | +) -> OperatingSystem: |
| 83 | + if not available_laptops: |
| 84 | + raise ValueError("No operating systems available") |
| 85 | + max_len: int = -1 |
| 86 | + most_available: OperatingSystem |
| 87 | + for key, val in available_laptops.items(): |
| 88 | + if len(val) > max_len: |
| 89 | + max_len = len(val) |
| 90 | + most_available = key |
| 91 | + |
| 92 | + return most_available |
| 93 | + |
| 94 | + |
| 95 | +def main() -> None: |
| 96 | + name: str = input("Name: ") |
| 97 | + age: int = int(input("Age: ")) |
| 98 | + |
| 99 | + print("""choose an operating system: |
| 100 | + 1.Ubuntu |
| 101 | + 2,Arch Linux |
| 102 | + 3.macOs""") |
| 103 | + choice: int = int(input("Enter Your choice: ")) |
| 104 | + os_map: dict[int, OperatingSystem] = { |
| 105 | + 1: OperatingSystem.UBUNTU, |
| 106 | + 2: OperatingSystem.ARCH, |
| 107 | + 3: OperatingSystem.MACOS, |
| 108 | + } |
| 109 | + |
| 110 | + preferred_operating_system: OperatingSystem = os_map[choice] |
| 111 | + |
| 112 | + person1: Person = Person(name, age, preferred_operating_system) |
| 113 | + |
| 114 | + laptops_by_operating_system = group_laptops_by_operating_system(laptops) |
| 115 | + matching_laptop_count: int = how_many_match(person1, laptops_by_operating_system) |
| 116 | + print("We have", matching_laptop_count, "matches") |
| 117 | + most_available_os: OperatingSystem = most_available_operating_system( |
| 118 | + laptops_by_operating_system |
| 119 | + ) |
| 120 | + if person1.preferred_operating_system != most_available_os: |
| 121 | + print("Are you willing to accept ", most_available_os.value, "instead") |
| 122 | + |
| 123 | + |
| 124 | +if __name__ == "__main__": |
| 125 | + main() |
0 commit comments