1+ from dataclasses import dataclass
2+ from enum import Enum
3+ from typing import List
4+ import sys
5+
6+
7+ class OperatingSystem (Enum ):
8+ MACOS = "macOS"
9+ ARCH = "Arch Linux"
10+ UBUNTU = "Ubuntu"
11+
12+
13+ @dataclass (frozen = True )
14+ class Person :
15+ name : str
16+ age : int
17+ preferred_operating_system : OperatingSystem
18+
19+
20+ @dataclass (frozen = True )
21+ class Laptop :
22+ id : int
23+ manufacturer : str
24+ model : str
25+ screen_size_in_inches : float
26+ operating_system : OperatingSystem
27+
28+
29+ laptops = [
30+ Laptop (1 , "Dell" , "XPS" , 13 , OperatingSystem .ARCH ),
31+ Laptop (2 , "Dell" , "XPS" , 15 , OperatingSystem .UBUNTU ),
32+ Laptop (3 , "Dell" , "XPS" , 15 , OperatingSystem .UBUNTU ),
33+ Laptop (4 , "Apple" , "MacBook" , 13 , OperatingSystem .MACOS ),
34+ ]
35+
36+
37+ # Read name
38+ name = input ("Enter your name: " )
39+
40+ # Read and convert age
41+ try :
42+ age = int (input ("Enter your age: " ))
43+ except ValueError :
44+ print ("Error: age must be a whole number." , file = sys .stderr )
45+ sys .exit (1 )
46+
47+ # Read and convert operating system
48+ print ("Choose an operating system:" )
49+ print ("- Ubuntu" )
50+ print ("- Arch Linux" )
51+ print ("- macOS" )
52+
53+ os_input = input ("Preferred operating system: " )
54+
55+ try :
56+ preferred_os = OperatingSystem (os_input )
57+ except ValueError :
58+ print ("Error: invalid operating system." , file = sys .stderr )
59+ sys .exit (1 )
60+
61+ # Create the person
62+ person = Person (name , age , preferred_os )
63+
64+ # Find matching laptops
65+ matching = [
66+ laptop
67+ for laptop in laptops
68+ if laptop .operating_system == person .preferred_operating_system
69+ ]
70+
71+ print (
72+ f"\n The library has { len (matching )} laptop(s) running "
73+ f"{ person .preferred_operating_system .value } ."
74+ )
75+
76+ # Count laptops for each operating system
77+ counts = {}
78+
79+ for laptop in laptops :
80+ os = laptop .operating_system
81+ counts [os ] = counts .get (os , 0 ) + 1
82+
83+ best_os = max (counts , key = counts .get )
84+
85+ if best_os != person .preferred_operating_system :
86+ print (
87+ f"If you're willing to accept { best_os .value } , "
88+ f"there are { counts [best_os ]} laptops available."
89+ )
0 commit comments