1+ from dataclasses import dataclass
2+ from enum import Enum
3+ import sys
4+
5+
6+ class OperatingSystem (Enum ):
7+ MACOS = "macOS"
8+ ARCH = "Arch Linux"
9+ UBUNTU = "Ubuntu"
10+
11+
12+ @dataclass (frozen = True )
13+ class Person :
14+ name : str
15+ age : int
16+ preferred_operating_system : OperatingSystem
17+
18+
19+ @dataclass (frozen = True )
20+ class Laptop :
21+ id : int
22+ manufacturer : str
23+ model : str
24+ screen_size_in_inches : float
25+ operating_system : OperatingSystem
26+
27+
28+ laptops = [
29+ Laptop (1 , "Dell" , "XPS" , 13 , OperatingSystem .ARCH ),
30+ Laptop (2 , "Dell" , "XPS" , 15 , OperatingSystem .UBUNTU ),
31+ Laptop (3 , "Dell" , "XPS" , 15 , OperatingSystem .UBUNTU ),
32+ Laptop (4 , "Apple" , "MacBook" , 13 , OperatingSystem .MACOS ),
33+ ]
34+
35+
36+ # Get the person's name
37+ name = input ("What is your name? " )
38+
39+
40+ # Get and convert age
41+ try :
42+ age = int (input ("What is your age? " ))
43+ except ValueError :
44+ print ("Age must be a number." , file = sys .stderr )
45+ sys .exit (1 )
46+
47+
48+ # Get and convert operating system
49+ try :
50+ os_input = input ("What is your preferred operating system? " )
51+ operating_system = OperatingSystem (os_input )
52+ except ValueError :
53+ print ("Invalid operating system." , file = sys .stderr )
54+ sys .exit (1 )
55+
56+
57+ person = Person (
58+ name = name ,
59+ age = age ,
60+ preferred_operating_system = operating_system ,
61+ )
62+
63+
64+ # Count laptops with the person's preferred OS
65+ number_available = sum (
66+ laptop .operating_system == person .preferred_operating_system
67+ for laptop in laptops
68+ )
69+
70+
71+ print (
72+ f"We have { number_available } laptop(s) with "
73+ f"{ person .preferred_operating_system .value } ."
74+ )
75+
76+
77+ # Find the operating system with the most laptops
78+ counts = {}
79+
80+ for os in OperatingSystem :
81+ counts [os ] = sum (
82+ laptop .operating_system == os
83+ for laptop in laptops
84+ )
85+
86+
87+ most_available_os = max (counts , key = counts .get )
88+
89+
90+ if most_available_os != person .preferred_operating_system :
91+ if counts [most_available_os ] > number_available :
92+ print (
93+ f"If you are willing to use { most_available_os .value } , "
94+ f"you are more likely to get a laptop because we have "
95+ f"{ counts [most_available_os ]} available."
96+ )
0 commit comments