|
| 1 | +from dataclasses import dataclass |
| 2 | +from enum import Enum |
| 3 | +from typing import List |
| 4 | +import sys |
| 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: 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 | + |
| 27 | +def find_possible_laptops(laptops: List[Laptop], person: Person) -> List[Laptop]: |
| 28 | + possible_laptops = [] |
| 29 | + for laptop in laptops: |
| 30 | + if laptop.operating_system == person.preferred_operating_system: |
| 31 | + possible_laptops.append(laptop) |
| 32 | + return possible_laptops |
| 33 | + |
| 34 | + |
| 35 | + |
| 36 | +laptops = [ |
| 37 | + Laptop(id=1, manufacturer="Dell", model="XPS", screen_size_in_inches=13, operating_system=OperatingSystem.ARCH), |
| 38 | + Laptop(id=2, manufacturer="Dell", model="XPS", screen_size_in_inches=15, operating_system=OperatingSystem.UBUNTU), |
| 39 | + Laptop(id=3, manufacturer="Dell", model="XPS", screen_size_in_inches=15, operating_system=OperatingSystem.UBUNTU), |
| 40 | + Laptop(id=4, manufacturer="Apple", model="macBook", screen_size_in_inches=13, operating_system=OperatingSystem.MACOS), |
| 41 | +] |
| 42 | + |
| 43 | + |
| 44 | +name = input("Enter your name: ") |
| 45 | + |
| 46 | +try: |
| 47 | + age = int(input("Enter your age: ")) |
| 48 | +except ValueError: |
| 49 | + print("Invalid age. Must be number!", file=sys.stderr) |
| 50 | + sys.exit(1) |
| 51 | + |
| 52 | +os_input = input("Enter preferred OS (macOS / Arch Linux / Ubuntu): ") |
| 53 | + |
| 54 | +try: |
| 55 | + preferred_os = OperatingSystem(os_input) |
| 56 | +except ValueError: |
| 57 | + print("Invalid operating system.", file=sys.stderr) |
| 58 | + sys.exit(1) |
| 59 | + |
| 60 | +person = Person(name=name, age=age, preferred_operating_system=preferred_os) |
| 61 | + |
| 62 | +matching = find_possible_laptops(laptops, person) |
| 63 | + |
| 64 | +print(f"\nWe have {len(matching)} laptop(s) with {person.preferred_operating_system.value}.") |
| 65 | + |
| 66 | +# Compare with other OS availability |
| 67 | +os_counts = {} |
| 68 | + |
| 69 | +for laptop in laptops: |
| 70 | + os_counts[laptop.operating_system] = os_counts.get(laptop.operating_system, 0) + 1 |
| 71 | + |
| 72 | +best_os = max(os_counts, key=os_counts.get) |
| 73 | + |
| 74 | +if best_os != person.preferred_operating_system: |
| 75 | + print( |
| 76 | + f"If you're flexible, {best_os.value} has more laptops available " |
| 77 | + f"({os_counts[best_os]} total)." |
| 78 | + ) |
0 commit comments