|
| 1 | +''' |
| 2 | +Write a program which: |
| 3 | +1.Already has a list of Laptops that a library has to lend out. |
| 4 | +2.Accepts user input to create a new Person - it should use the input function to |
| 5 | +read a person's name, age, and preferred operating system. |
| 6 | +3.Tells the user how many laptops the library has that have that operating system. |
| 7 | +4.If there is an operating system that has more laptops available, tells the user |
| 8 | +that if they're willing to accept that operating system they're more likely to get a laptop. |
| 9 | +
|
| 10 | +You should convert the age and preferred operating system input from the user |
| 11 | +into more constrained types as quickly as possible, and should output errors to |
| 12 | +stderr and terminate the program with a non-zero exit code if the user input bad values. |
| 13 | +''' |
| 14 | + |
| 15 | +from dataclasses import dataclass |
| 16 | +from enum import Enum |
| 17 | +from typing import List |
| 18 | + |
| 19 | +class OperatingSystem(Enum): |
| 20 | + MACOS = "macOS" |
| 21 | + ARCH = "Arch Linux" |
| 22 | + UBUNTU = "Ubuntu" |
| 23 | + |
| 24 | +@dataclass(frozen=True) |
| 25 | +class Person: |
| 26 | + name: str |
| 27 | + age: int |
| 28 | + preferred_operating_system: OperatingSystem |
| 29 | + |
| 30 | + |
| 31 | +@dataclass(frozen=True) |
| 32 | +class Laptop: |
| 33 | + id: int |
| 34 | + manufacturer: str |
| 35 | + model: str |
| 36 | + screen_size_in_inches: float |
| 37 | + operating_system: OperatingSystem |
| 38 | + |
| 39 | + |
| 40 | +def find_possible_laptops(laptops: List[Laptop], person: Person) -> List[Laptop]: |
| 41 | + possible_laptops = [] |
| 42 | + for laptop in laptops: |
| 43 | + if laptop.operating_system == person.preferred_operating_system: |
| 44 | + possible_laptops.append(laptop) |
| 45 | + return possible_laptops |
| 46 | + |
| 47 | + |
| 48 | +people = [ |
| 49 | + Person(name="Imran", age=22, preferred_operating_system=OperatingSystem.UBUNTU), |
| 50 | + Person(name="Eliza", age=34, preferred_operating_system=OperatingSystem.ARCH), |
| 51 | +] |
| 52 | + |
| 53 | +laptops = [ |
| 54 | + Laptop(id=1, manufacturer="Dell", model="XPS", screen_size_in_inches=13, operating_system=OperatingSystem.ARCH), |
| 55 | + Laptop(id=2, manufacturer="Dell", model="XPS", screen_size_in_inches=15, operating_system=OperatingSystem.UBUNTU), |
| 56 | + Laptop(id=3, manufacturer="Dell", model="XPS", screen_size_in_inches=15, operating_system=OperatingSystem.UBUNTU), |
| 57 | + Laptop(id=4, manufacturer="Apple", model="macBook", screen_size_in_inches=13, operating_system=OperatingSystem.MACOS), |
| 58 | +] |
| 59 | + |
| 60 | +print("Library laptops:") # 1. list of laptops |
| 61 | +for laptop in laptops: |
| 62 | + print(f"- ID:{laptop.id} {laptop.operating_system.value} - {laptop.manufacturer} {laptop.model} Size: {laptop.screen_size_in_inches}") |
| 63 | + |
| 64 | +# receiving values from input to create person: |
| 65 | +name = input("Enter your name: ") |
| 66 | +age = int(input("Enter your age: ")) |
| 67 | + |
| 68 | +print("Choose preferred operating system:") |
| 69 | +print("1. macOS") |
| 70 | +print("2. Arch Linux") |
| 71 | +print("3. Ubuntu") |
| 72 | +choice = input(">> ") |
| 73 | + |
| 74 | +os_map = { |
| 75 | + "1": OperatingSystem.MACOS, |
| 76 | + "2": OperatingSystem.ARCH, |
| 77 | + "3": OperatingSystem.UBUNTU, |
| 78 | +} |
| 79 | + |
| 80 | +preferred_os = os_map.get(choice) |
| 81 | + |
| 82 | +person = Person(name=name, age=age, preferred_operating_system=preferred_os) |
| 83 | + |
| 84 | +print("Created person:", person) |
| 85 | +# counts how many laptops there are with that OS |
| 86 | +matches_count = sum( |
| 87 | + 1 for laptop in laptops |
| 88 | + if laptop.operating_system == person.preferred_operating_system |
| 89 | +) |
| 90 | +print(f"There are {matches_count} laptop(s) with {person.preferred_operating_system.value}.") |
| 91 | + |
| 92 | +count_laptops_by_os = { |
| 93 | + os_type: sum(1 for laptop in laptops if laptop.operating_system == os_type) |
| 94 | + for os_type in OperatingSystem |
| 95 | +} |
| 96 | + |
| 97 | +best_os = max(count_laptops_by_os, key = count_laptops_by_os.get) |
| 98 | +best_os_count = count_laptops_by_os[best_os] |
| 99 | + |
| 100 | +if best_os != person.preferred_operating_system: |
| 101 | + print( |
| 102 | + f"There are more laptops available with {best_os.value} " |
| 103 | + f"({best_os_count} laptops). If you're willing to use that OS, " |
| 104 | + "you're more likely to get a laptop." |
| 105 | + ) |
| 106 | +else: |
| 107 | + print("Good choice! Your preferred OS has the highest availability.") |
| 108 | + |
0 commit comments