Skip to content

Commit cfa95d6

Browse files
committed
Add laptop library with enum validation and OS availability check
1 parent a5faf8b commit cfa95d6

1 file changed

Lines changed: 90 additions & 0 deletions

File tree

Sprint5/e-num.py

Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
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+
@dataclass(frozen=True)
13+
class Person:
14+
name: str
15+
age: int
16+
preferred_os: 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+
#library of laptops
28+
laptops = [
29+
Laptop(id=1, manufacturer="Dell", model="XPS", screen_size_in_inches=13, operating_system=OperatingSystem.ARCH),
30+
Laptop(id=2, manufacturer="Dell", model="XPS", screen_size_in_inches=15, operating_system=OperatingSystem.UBUNTU),
31+
Laptop(id=3, manufacturer="Dell", model="XPS", screen_size_in_inches=15, operating_system=OperatingSystem.UBUNTU),
32+
Laptop(id=4, manufacturer="Apple", model="macBook", screen_size_in_inches=13, operating_system=OperatingSystem.MACOS),
33+
]
34+
35+
def find_possible_laptops(laptops: List[Laptop], person: Person) -> List[Laptop]:
36+
possible_laptops = []
37+
for laptop in laptops:
38+
if laptop.operating_system == person.preferred_os:
39+
possible_laptops.append(laptop)
40+
return possible_laptops # returns an array of possible laptops for the person
41+
42+
#accept userinput
43+
name = input("Enter your name: ").strip()
44+
age_str = input("Enter your age: ").strip() #input always returns a str, we need to convert this to an integer
45+
os_str = input("Enter your preferred operating system: ").strip()
46+
# Validate age
47+
try:
48+
age = int(age_str)
49+
except ValueError:
50+
print("Error: Age must be a number.", file= sys.stderr)
51+
sys.exit(1)
52+
53+
# Validate OS
54+
try:
55+
preferred_os = OperatingSystem(os_str)
56+
except ValueError:
57+
print("Error: Invalid operating system.", file=sys.stderr)
58+
sys.exit(1)
59+
60+
#Create a person after validation
61+
person = Person(name = name, age = age, preferred_os =preferred_os)
62+
#finding laptops for this person
63+
possible = find_possible_laptops(laptops, person)
64+
print(f"The laptop library has {len(possible)} with {preferred_os.value}.")
65+
66+
# Start with an empty dictionary
67+
os_counts: dict[OperatingSystem, int] = {}
68+
69+
for laptop in laptops:
70+
os = laptop.operating_system
71+
# If we've seen this OS before, add 1, otherwise start at 1
72+
if os in os_counts:
73+
os_counts[os] += 1
74+
else:
75+
os_counts[os] = 1
76+
77+
best_os = None
78+
best_count = -1
79+
80+
# Loop through each (key, value) pair
81+
for pair in os_counts.items():
82+
os = pair[0] # the key (operating system)
83+
count = pair[1] # the value (number of laptops)
84+
85+
# Check if this count is bigger than the best so far
86+
if count > best_count:
87+
best_os = os
88+
best_count = count
89+
90+
print("Best OS:", best_os, "with", best_count, "laptops")

0 commit comments

Comments
 (0)