|
| 1 | +# ------------------------ |
| 2 | +# Write a program which: |
| 3 | + |
| 4 | +# Already has a list of Laptops that a library has to lend out. |
| 5 | +# Accepts user input to create a new Person - it should use the input function to read a person’s name, age, and preferred operating system. |
| 6 | +# Tells the user how many laptops the library has that have that operating system. |
| 7 | +# If there is an operating system that has more laptops available, tells the user that if they’re willing to accept that operating system they’re more likely to get a laptop. |
| 8 | +# You should convert the age and preferred operating system input from the user into more constrained types as quickly as possible, and should output errors to stderr and terminate the program with a non-zero exit code if the user input bad values. |
| 9 | +# ------------------------ |
| 10 | + |
| 11 | +# A. |
| 12 | + |
| 13 | +import sys |
| 14 | +from dataclasses import dataclass |
| 15 | +from enum import Enum |
| 16 | +from typing import List |
| 17 | + |
| 18 | + |
| 19 | +class OperatingSystem(Enum): |
| 20 | + MACOS = "macOS" |
| 21 | + ARCH = "Arch Linux" |
| 22 | + UBUNTU = "Ubuntu" |
| 23 | + |
| 24 | + |
| 25 | +@dataclass(frozen=True) |
| 26 | +class Person: |
| 27 | + name: str |
| 28 | + age: int |
| 29 | + preferred_operating_system: OperatingSystem |
| 30 | + |
| 31 | + |
| 32 | +@dataclass(frozen=True) |
| 33 | +class Laptop: |
| 34 | + id: int |
| 35 | + manufacturer: str |
| 36 | + model: str |
| 37 | + screen_size_in_inches: float |
| 38 | + operating_system: OperatingSystem |
| 39 | + |
| 40 | + |
| 41 | +def find_possible_laptops(laptops: List[Laptop], person: Person) -> List[Laptop]: |
| 42 | + possible_laptops: List[Laptop] = [] |
| 43 | + for laptop in laptops: |
| 44 | + if laptop.operating_system == person.preferred_operating_system: |
| 45 | + possible_laptops.append(laptop) |
| 46 | + return possible_laptops |
| 47 | + |
| 48 | + |
| 49 | +laptops = [ |
| 50 | + Laptop(id=1, manufacturer="Dell", model="XPS", |
| 51 | + screen_size_in_inches=13, operating_system=OperatingSystem.ARCH), |
| 52 | + Laptop(id=2, manufacturer="Dell", model="XPS", |
| 53 | + screen_size_in_inches=15, operating_system=OperatingSystem.UBUNTU), |
| 54 | + Laptop(id=3, manufacturer="Dell", model="XPS", |
| 55 | + screen_size_in_inches=15, operating_system=OperatingSystem.UBUNTU), |
| 56 | + Laptop(id=4, manufacturer="Apple", model="macBook", |
| 57 | + screen_size_in_inches=13, operating_system=OperatingSystem.MACOS), |
| 58 | +] |
| 59 | + |
| 60 | +try: |
| 61 | + user_name = input('Enter your name: ') |
| 62 | + if not user_name.strip(): |
| 63 | + raise ValueError("Name cannot be empty") |
| 64 | +except ValueError: |
| 65 | + sys.stderr.write("Error: Please provide a valid name\n") |
| 66 | + sys.exit(1) |
| 67 | + |
| 68 | +try: |
| 69 | + user_age_input = input('Enter your age: ') |
| 70 | + user_age = int(user_age_input) |
| 71 | + if user_age < 0: |
| 72 | + raise ValueError("Age cannot be negative") |
| 73 | +except ValueError: |
| 74 | + sys.stderr.write("Error: Please provide a valid age\n") |
| 75 | + sys.exit(1) |
| 76 | + |
| 77 | +try: |
| 78 | + user_os_input = input( |
| 79 | + 'Enter your preferred operating system (macOS, Arch Linux, Ubuntu): ') |
| 80 | + user_os = OperatingSystem(user_os_input) |
| 81 | +except ValueError: |
| 82 | + sys.stderr.write("Error: Please enter a valid operating system\n") |
| 83 | + sys.exit(1) |
| 84 | + |
| 85 | +person = Person(name=user_name, age=user_age, |
| 86 | + preferred_operating_system=user_os) |
| 87 | + |
| 88 | +possible_laptops = find_possible_laptops(laptops, person) |
| 89 | +print(f"We have {len(possible_laptops)} laptop(s) with {user_os.value}") |
| 90 | + |
| 91 | +# Find OS with most laptops |
| 92 | +os_counts: dict[OperatingSystem, int] = {} |
| 93 | +for laptop in laptops: |
| 94 | + os_counts[laptop.operating_system] = os_counts.get( |
| 95 | + laptop.operating_system, 0) + 1 |
| 96 | + |
| 97 | +best_os = max(os_counts, key=lambda os: os_counts[os]) |
| 98 | +if best_os != user_os and os_counts[best_os] > len(possible_laptops): |
| 99 | + print( |
| 100 | + f"If you're willing to accept {best_os.value}, you're more likely to get a laptop ({os_counts[best_os]} available)") |
0 commit comments