Skip to content

Commit a8204fe

Browse files
committed
Completed Enums exercise.
1 parent d283227 commit a8204fe

1 file changed

Lines changed: 109 additions & 0 deletions

File tree

prep exercises/Enums exercise.py

Lines changed: 109 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,109 @@
1+
from dataclasses import dataclass
2+
from enum import Enum
3+
from typing import List
4+
import sys
5+
6+
class OperatingSystem(Enum):
7+
MACOS = "macOS"
8+
ARCH = "Arch Linux"
9+
UBUNTU = "Ubuntu"
10+
11+
@dataclass(frozen=True)
12+
class Person:
13+
name: str
14+
age: int
15+
preferred_operating_system: OperatingSystem
16+
17+
18+
@dataclass(frozen=True)
19+
class Laptop:
20+
id: int
21+
manufacturer: str
22+
model: str
23+
screen_size_in_inches: float
24+
operating_system: OperatingSystem
25+
26+
27+
def find_possible_laptops(laptops: List[Laptop], person: Person) -> List[Laptop]:
28+
possible_laptops = []
29+
for laptop in laptops:
30+
if laptop.operating_system == person.preferred_operating_system:
31+
possible_laptops.append(laptop)
32+
return possible_laptops
33+
34+
35+
people = [
36+
Person(name="Imran", age=22, preferred_operating_system=OperatingSystem.UBUNTU),
37+
Person(name="Eliza", age=34, preferred_operating_system=OperatingSystem.ARCH),
38+
]
39+
40+
laptops = [
41+
Laptop(id=1, manufacturer="Dell", model="XPS", screen_size_in_inches=13, operating_system=OperatingSystem.ARCH),
42+
Laptop(id=2, manufacturer="Dell", model="XPS", screen_size_in_inches=15, operating_system=OperatingSystem.UBUNTU),
43+
Laptop(id=3, manufacturer="Dell", model="XPS", screen_size_in_inches=15, operating_system=OperatingSystem.UBUNTU),
44+
Laptop(id=4, manufacturer="Apple", model="macBook", screen_size_in_inches=13, operating_system=OperatingSystem.MACOS),
45+
]
46+
47+
# for person in people:
48+
# possible_laptops = find_possible_laptops(laptops, person)
49+
# print(f"Possible laptops for {person.name}: {possible_laptops}")
50+
51+
name = input("Please enter your name: ")
52+
53+
def get_age() -> int:
54+
while True:
55+
try:
56+
age_string = input("Please enter your age, you must be >= 18 years old: ")
57+
age = int(age_string)
58+
59+
if age < 18:
60+
print("You must be 18+.")
61+
continue
62+
63+
return age
64+
except ValueError:
65+
print("Please enter a valid number.")
66+
67+
age = get_age()
68+
69+
def get_preferred_operating_system() -> OperatingSystem:
70+
while True:
71+
preferred_operating_system_input = input("Your preferred operating system (macOS, Ubuntu or Arch Linux): ")
72+
73+
try:
74+
normalised_value = preferred_operating_system_input.strip()
75+
operating_system = OperatingSystem(normalised_value)
76+
return operating_system
77+
except ValueError:
78+
print(f"Sorry, we don't have {preferred_operating_system_input}. Please choose from macOS, Ubuntu or Arch Linux.")
79+
80+
operating_system = get_preferred_operating_system()
81+
82+
person = Person(name=name, age=age, preferred_operating_system=operating_system)
83+
people.append(person)
84+
85+
def find_laptop() -> None:
86+
matching_laptops = []
87+
non_matching_laptops = []
88+
89+
for laptop in laptops:
90+
if laptop.operating_system == person.preferred_operating_system:
91+
matching_laptops.append(laptop)
92+
else:
93+
non_matching_laptops.append(laptop)
94+
95+
if matching_laptops:
96+
count = len(matching_laptops)
97+
print(f"Congratulations, we have found {count} matching laptops!")
98+
99+
other_laptops_count = len(non_matching_laptops)
100+
101+
if other_laptops_count > count:
102+
print(f"We have found {other_laptops_count} laptops with other operating systems. You might be interested in switching to a different operating system.")
103+
104+
sys.exit(0)
105+
else:
106+
sys.exit("Sorry, we couldn't find any matching laptops!")
107+
108+
109+
find_laptop()

0 commit comments

Comments
 (0)