|
| 1 | +from dataclasses import dataclass |
| 2 | +from typing import List, Tuple |
| 3 | +from enum import Enum |
| 4 | +import sys |
| 5 | +from collections import Counter |
| 6 | + |
| 7 | + |
| 8 | +class OperatingSystem(Enum): |
| 9 | + MACOS = "macOS" |
| 10 | + ARCH = "Arch Linux" |
| 11 | + UBUNTU = "Ubuntu" |
| 12 | + |
| 13 | + |
| 14 | +@dataclass(frozen=True) |
| 15 | +class Person: |
| 16 | + name: str |
| 17 | + age: int |
| 18 | + preferred_operating_system: OperatingSystem |
| 19 | + |
| 20 | + |
| 21 | +@dataclass(frozen=True) |
| 22 | +class Laptop: |
| 23 | + id: int |
| 24 | + manufacturer: str |
| 25 | + model: str |
| 26 | + screen_size_in_inches: float |
| 27 | + operating_system: OperatingSystem |
| 28 | + |
| 29 | + |
| 30 | +laptops = [ |
| 31 | + Laptop( |
| 32 | + id=1, |
| 33 | + manufacturer="Dell", |
| 34 | + model="XPS", |
| 35 | + screen_size_in_inches=13, |
| 36 | + operating_system=OperatingSystem.ARCH, |
| 37 | + ), |
| 38 | + Laptop( |
| 39 | + id=2, |
| 40 | + manufacturer="Dell", |
| 41 | + model="XPS", |
| 42 | + screen_size_in_inches=15, |
| 43 | + operating_system=OperatingSystem.UBUNTU, |
| 44 | + ), |
| 45 | + Laptop( |
| 46 | + id=3, |
| 47 | + manufacturer="Dell", |
| 48 | + model="XPS", |
| 49 | + screen_size_in_inches=15, |
| 50 | + operating_system=OperatingSystem.UBUNTU, |
| 51 | + ), |
| 52 | + Laptop( |
| 53 | + id=4, |
| 54 | + manufacturer="Apple", |
| 55 | + model="macBook", |
| 56 | + screen_size_in_inches=13, |
| 57 | + operating_system=OperatingSystem.MACOS, |
| 58 | + ), |
| 59 | +] |
| 60 | + |
| 61 | + |
| 62 | +users = [] |
| 63 | + |
| 64 | + |
| 65 | +def find_possible_laptops(laptops: List[Laptop], person: Person) -> List[Laptop]: |
| 66 | + possible_laptops = [] |
| 67 | + for laptop in laptops: |
| 68 | + if laptop.operating_system == person.preferred_operating_system: |
| 69 | + possible_laptops.append(laptop) |
| 70 | + return possible_laptops |
| 71 | + |
| 72 | + |
| 73 | +def input_validation() -> Tuple[str, int, OperatingSystem]: |
| 74 | + input_user_name = input("Enter your name: ") |
| 75 | + if not isinstance(input_user_name, str): |
| 76 | + sys.stderr.write("Name must be a string") |
| 77 | + sys.exit(1) |
| 78 | + |
| 79 | + try: |
| 80 | + input_user_age = int(input("Enter your age: ")) |
| 81 | + except ValueError: |
| 82 | + sys.stderr.write("Age must be an integer") |
| 83 | + sys.exit(1) |
| 84 | + |
| 85 | + input_preferred_operating_system = input( |
| 86 | + "Enter your preferred operating system (macOS/Arch Linux/Ubuntu): " |
| 87 | + ) |
| 88 | + |
| 89 | + try: |
| 90 | + preferred_os = OperatingSystem(input_preferred_operating_system) |
| 91 | + except ValueError: |
| 92 | + sys.stderr.write("Wrong OS") |
| 93 | + sys.exit(1) |
| 94 | + |
| 95 | + return input_user_name, input_user_age, preferred_os |
| 96 | + |
| 97 | + |
| 98 | +def count_operating_systems(laptops: List[Laptop]) -> Counter[OperatingSystem]: |
| 99 | + return Counter(laptop.operating_system for laptop in laptops) |
| 100 | + |
| 101 | + |
| 102 | +def recommend_os(user: Person, laptops: List[Laptop]) -> None: |
| 103 | + os_counts = count_operating_systems(laptops) |
| 104 | + most_common_os, most_common_count = os_counts.most_common(1)[0] |
| 105 | + |
| 106 | + user_count = os_counts[user.preferred_operating_system] |
| 107 | + |
| 108 | + if ( |
| 109 | + most_common_os != user.preferred_operating_system |
| 110 | + and user_count < most_common_count |
| 111 | + ): |
| 112 | + print(f"More laptops are available with {most_common_os.value}.") |
| 113 | + |
| 114 | + |
| 115 | +def laptop() -> None: |
| 116 | + input_user_name, input_user_age, input_preferred_operating_system = ( |
| 117 | + input_validation() |
| 118 | + ) |
| 119 | + |
| 120 | + users.append( |
| 121 | + Person( |
| 122 | + name=input_user_name, |
| 123 | + age=input_user_age, |
| 124 | + preferred_operating_system=OperatingSystem( |
| 125 | + input_preferred_operating_system |
| 126 | + ), |
| 127 | + ), |
| 128 | + ) |
| 129 | + for user in users: |
| 130 | + possible_laptops = find_possible_laptops(laptops, user) |
| 131 | + print( |
| 132 | + f"Possible laptops for {user.name}:\n{'\n'.join(map(str, possible_laptops))}", |
| 133 | + ) |
| 134 | + recommend_os(user, laptops) |
| 135 | + |
| 136 | + |
| 137 | +laptop() |
0 commit comments