Skip to content

Commit 7e0352e

Browse files
committed
enum exer.
1 parent 2090e74 commit 7e0352e

1 file changed

Lines changed: 105 additions & 0 deletions

File tree

enums.py

Lines changed: 105 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,105 @@
1+
from dataclasses import dataclass
2+
from enum import Enum
3+
from typing import List, Dict
4+
import sys
5+
6+
7+
8+
9+
class OperatingSystem(Enum):
10+
MACOS = "macOS"
11+
ARCH = "Arch Linux"
12+
UBUNTU = "Ubuntu"
13+
14+
@dataclass(frozen=True)
15+
class Person:
16+
name: str
17+
age: int
18+
preferred_operating_system: OperatingSystem
19+
20+
21+
@dataclass(frozen=True)
22+
class Laptop:
23+
id: int
24+
manufacturer: str
25+
model: str
26+
screen_size_in_inches: float
27+
operating_system: OperatingSystem
28+
29+
30+
def count_laptops(laptops: List[Laptop]) -> Dict[OperatingSystem, int]:
31+
number_eachOS_laptops: Dict[OperatingSystem, int] = {
32+
OperatingSystem.MACOS: 0,
33+
OperatingSystem.ARCH: 0,
34+
OperatingSystem.UBUNTU: 0}
35+
for laptop in laptops:
36+
number_eachOS_laptops[laptop.operating_system] +=1
37+
return number_eachOS_laptops
38+
39+
40+
def count_possible_laptops(laptops: List[Laptop], person: Person) -> int:
41+
possible_laptops: List[Laptop] =[]
42+
for laptop in laptops:
43+
if laptop.operating_system == person.preferred_operating_system:
44+
possible_laptops.append(laptop)
45+
number_possible_laptops = len(possible_laptops)
46+
return number_possible_laptops
47+
48+
def chose_alternative_laptops(laptops: List[Laptop], person: Person) -> Dict[OperatingSystem, int]:
49+
number_possible_laptops = count_possible_laptops(laptops, person)
50+
number_eachOS_laptops = count_laptops(laptops)
51+
preferred_os = person.preferred_operating_system
52+
alternative_laptops: Dict[OperatingSystem, int] = {}
53+
for eachOS, count in number_eachOS_laptops.items():
54+
if eachOS == preferred_os:
55+
continue
56+
if count > number_possible_laptops:
57+
alternative_laptops[eachOS] = count
58+
if len(alternative_laptops) != 0:
59+
print(f"There is an operating system that has more laptops available.If you’re willing to accept them, there is a list: {alternative_laptops}.")
60+
return alternative_laptops
61+
else:
62+
print("There is not an operating system that has more laptops available.")
63+
return alternative_laptops
64+
65+
while True:
66+
user_name = input("Type your name: ").strip()
67+
if len(user_name) < 3:
68+
print(f"Error, {user_name} is not valid. Try again, length should be more than 3 characters.")
69+
continue
70+
break
71+
72+
while True:
73+
user_age = input("Type your age: ").strip()
74+
try:
75+
user_age_int = int(user_age)
76+
if user_age_int < 18:
77+
raise ValueError
78+
break
79+
except ValueError:
80+
print("Invalid age, try again! Borrowing allowed from 18 years old.")
81+
82+
available_os = [os.value for os in OperatingSystem]
83+
print("Available OSs are: ", ",".join(available_os))
84+
user_operating_system = input("Type operating system: ").strip()
85+
if user_operating_system not in available_os:
86+
print(f"Error, {user_operating_system} is not in available list\n"
87+
f"Available OSs are: {','.join(available_os)}", file=sys.stderr)
88+
sys.exit(1)
89+
90+
preferred_operating_system = OperatingSystem(user_operating_system)
91+
92+
user = Person(name=user_name, age=user_age_int, preferred_operating_system=preferred_operating_system)
93+
94+
95+
laptops = [
96+
Laptop(id=1, manufacturer="Dell", model="XPS", screen_size_in_inches=13, operating_system=OperatingSystem.ARCH),
97+
Laptop(id=2, manufacturer="Dell", model="XPS", screen_size_in_inches=15, operating_system=OperatingSystem.UBUNTU),
98+
Laptop(id=3, manufacturer="Dell", model="XPS", screen_size_in_inches=15, operating_system=OperatingSystem.UBUNTU),
99+
Laptop(id=4, manufacturer="Apple", model="macBook", screen_size_in_inches=13, operating_system=OperatingSystem.MACOS),
100+
]
101+
102+
103+
possible_laptops = count_possible_laptops(laptops, user)
104+
print(f"Possible laptops for {user_name}: {possible_laptops}")
105+
alternative_laptops = chose_alternative_laptops(laptops, user)

0 commit comments

Comments
 (0)