|
| 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 | + |
| 10 | +@dataclass(frozen=True) |
| 11 | +class Person: |
| 12 | + name: str |
| 13 | + age: int |
| 14 | + preferred_operating_system: OperatingSystem |
| 15 | + |
| 16 | +@dataclass(frozen=True) |
| 17 | +class Laptop: |
| 18 | + id: int |
| 19 | + manufacturer: str |
| 20 | + model: str |
| 21 | + screen_size_in_inches: float |
| 22 | + operating_system: OperatingSystem |
| 23 | + |
| 24 | +laptops = [ |
| 25 | + Laptop(id=1, manufacturer="Dell", model="XPS", screen_size_in_inches=13, operating_system=OperatingSystem.ARCH), |
| 26 | + Laptop(id=2, manufacturer="Dell", model="XPS", screen_size_in_inches=15, operating_system=OperatingSystem.UBUNTU), |
| 27 | + Laptop(id=3, manufacturer="Dell", model="XPS", screen_size_in_inches=15, operating_system=OperatingSystem.UBUNTU), |
| 28 | + Laptop(id=4, manufacturer="Apple", model="macBook", screen_size_in_inches=13, operating_system=OperatingSystem.MACOS), |
| 29 | +] |
| 30 | + |
| 31 | +def count_laptops(laptops: List[Laptop], operating_system: OperatingSystem) -> int: |
| 32 | + count = 0 |
| 33 | + for laptop in laptops: |
| 34 | + if laptop.operating_system == operating_system: |
| 35 | + count += 1 |
| 36 | + return count |
| 37 | + |
| 38 | +def most_available_os(laptops: List[Laptop]) -> OperatingSystem: |
| 39 | + best_os = OperatingSystem.UBUNTU |
| 40 | + best_count = count_laptops(laptops, best_os) |
| 41 | + |
| 42 | + for os in OperatingSystem: |
| 43 | + current_count = count_laptops(laptops, os) |
| 44 | + if current_count > best_count: |
| 45 | + best_count = current_count |
| 46 | + best_os = os |
| 47 | + |
| 48 | + return best_os |
| 49 | + |
| 50 | +name = input("Enter your name: ") |
| 51 | +age = int(input("Enter your age: ")) |
| 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.") |
| 58 | + exit() |
| 59 | + |
| 60 | +person = Person(name, age, preferred_os) |
| 61 | + |
| 62 | +available = count_laptops(laptops, person.preferred_operating_system) |
| 63 | + |
| 64 | +print( |
| 65 | + f"\nThere are {available} {person.preferred_operating_system.value} laptop(s) available." |
| 66 | +) |
| 67 | + |
| 68 | +best_os = most_available_os(laptops) |
| 69 | + |
| 70 | +if best_os != person.preferred_operating_system: |
| 71 | + print( |
| 72 | + f"If you're willing to accept {best_os.value}, you're more likely to get a laptop." |
| 73 | + ) |
0 commit comments