Skip to content

Commit 1e84208

Browse files
committed
feat: iImplement the laptop exercise.
1 parent d3717ca commit 1e84208

1 file changed

Lines changed: 95 additions & 0 deletions

File tree

prep-exercises/enums.py

Lines changed: 95 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,95 @@
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+
Laptop(id=5, manufacturer="Dell", model="XPS", screen_size_in_inches=13, operating_system=OperatingSystem.ARCH)
46+
]
47+
48+
49+
user_name = input(f"PLease enter your name: ")
50+
51+
user_age = input(f"PLease enter your age: ")
52+
try :
53+
user_age_int = int(user_age)
54+
except ValueError :
55+
sys.stderr.write(f" Error: {user_age} should be an number")
56+
sys.exit(1)
57+
58+
user_os = input(f"PLease enter your preferred os: ")
59+
try:
60+
os_type = OperatingSystem(user_os)
61+
62+
except ValueError :
63+
sys.stderr.write(f"Error: {user_os} should be a valid OS")
64+
sys.exit(1)
65+
66+
67+
new_data = Person(user_name,user_age_int,os_type)
68+
amount_os =find_possible_laptops(laptops,new_data)
69+
len_os =len(amount_os)
70+
print(f"we found {len_os} available")
71+
72+
counts ={}
73+
for os in OperatingSystem:
74+
count =0
75+
76+
for laptop in laptops:
77+
if laptop.operating_system ==os:
78+
count+=1
79+
counts[os]=count
80+
81+
max_value= max(counts.values())
82+
better_choices = [max_value]
83+
84+
better_choices=[]
85+
for os, count in counts.items():
86+
if count == max_value:
87+
better_choices.append(os.value)
88+
89+
90+
multi_options = " OR ".join(better_choices)
91+
92+
if max_value > len_os:
93+
print(f"We have better choices, you should consider: {multi_options}")
94+
elif max_value==len_os :
95+
print(f"we have multiple choices: {multi_options}, however {os_type.value} is still a good choice.")

0 commit comments

Comments
 (0)