Skip to content

Commit 0c3a38d

Browse files
committed
enum exercise
1 parent 6e1b00a commit 0c3a38d

File tree

1 file changed

+78
-0
lines changed

1 file changed

+78
-0
lines changed

AYK/enum.py

Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
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_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+
def find_possible_laptops(laptops: List[Laptop], person: Person) -> List[Laptop]:
28+
return [laptop for laptop in laptops if laptop.operating_system == person.preferred_operating_system]
29+
30+
31+
laptops = [
32+
Laptop(id=1, manufacturer="Dell", model="XPS", screen_size_in_inches=13, operating_system=OperatingSystem.ARCH),
33+
Laptop(id=2, manufacturer="Dell", model="XPS", screen_size_in_inches=15, operating_system=OperatingSystem.UBUNTU),
34+
Laptop(id=3, manufacturer="Dell", model="XPS", screen_size_in_inches=15, operating_system=OperatingSystem.UBUNTU),
35+
Laptop(id=4, manufacturer="Apple", model="macBook", screen_size_in_inches=13, operating_system=OperatingSystem.MACOS),
36+
]
37+
38+
39+
name = input("Enter your name: ").strip()
40+
age_input = input("Enter your age: ").strip()
41+
age = int(age_input)
42+
if age <= 0:
43+
print("Age must be positive")
44+
print("Available Operating Systems: macOS, Arch Linux, Ubuntu")
45+
os_input = input("Enter your preferred operating system: ").strip().lower()
46+
47+
48+
os_map = {os.value.lower(): os for os in OperatingSystem}
49+
if os_input not in os_map:
50+
print(f"Invalid operating system '{os_input}'")
51+
preferred_os = os_map[os_input]
52+
53+
54+
sys.exit(1)
55+
56+
57+
person = Person(name=name, age=age, preferred_operating_system=preferred_os)
58+
59+
60+
matching_laptops = find_possible_laptops(laptops, person)
61+
print(f"\nHello {person.name}! There are {len(matching_laptops)} laptop(s) with {person.preferred_operating_system.value} available.")
62+
63+
64+
os_counts = {os: 0 for os in OperatingSystem}
65+
for laptop in laptops:
66+
os_counts[laptop.operating_system] += 1
67+
68+
69+
max_os = None
70+
max_count = 0
71+
72+
for os, count in os_counts.items():
73+
if count > max_count:
74+
max_os = os
75+
max_count = count
76+
77+
if max_os != person.preferred_operating_system and max_count > len(matching_laptops):
78+
print(f"Tip: There are more laptops available with {max_os.value}.")

0 commit comments

Comments
 (0)