Skip to content

Commit 535683f

Browse files
committed
Add enums-exercise.py: library laptop matching system with type-safe enums
- Create OperatingSystem Enum with MACOS, ARCH, and UBUNTU values - Define Person dataclass with preferred_operating_system as Enum type - Define Laptop dataclass with operating_system as Enum type - Implement find_possible_laptops() function to match laptops to person preferences - Add library inventory: 4 sample laptops with different operating systems - Add sample people with OS preferences - Demonstrate type-safe enum usage vs string-based approach - Display matching laptops for each person
1 parent 3c7f26a commit 535683f

1 file changed

Lines changed: 48 additions & 0 deletions

File tree

prep/enums-exercise.py

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
from dataclasses import dataclass
2+
from enum import Enum
3+
from typing import List
4+
5+
class OperatingSystem(Enum):
6+
MACOS = "macOS"
7+
ARCH = "Arch Linux"
8+
UBUNTU = "Ubuntu"
9+
10+
@dataclass(frozen=True)
11+
class Person:
12+
name: str
13+
age: int
14+
preferred_operating_system: OperatingSystem
15+
16+
17+
@dataclass(frozen=True)
18+
class Laptop:
19+
id: int
20+
manufacturer: str
21+
model: str
22+
screen_size_in_inches: float
23+
operating_system: OperatingSystem
24+
25+
26+
def find_possible_laptops(laptops: List[Laptop], person: Person) -> List[Laptop]:
27+
possible_laptops = []
28+
for laptop in laptops:
29+
if laptop.operating_system == person.preferred_operating_system:
30+
possible_laptops.append(laptop)
31+
return possible_laptops
32+
33+
34+
people = [
35+
Person(name="Imran", age=22, preferred_operating_system=OperatingSystem.UBUNTU),
36+
Person(name="Eliza", age=34, preferred_operating_system=OperatingSystem.ARCH),
37+
]
38+
39+
laptops = [
40+
Laptop(id=1, manufacturer="Dell", model="XPS", screen_size_in_inches=13, operating_system=OperatingSystem.ARCH),
41+
Laptop(id=2, manufacturer="Dell", model="XPS", screen_size_in_inches=15, operating_system=OperatingSystem.UBUNTU),
42+
Laptop(id=3, manufacturer="Dell", model="XPS", screen_size_in_inches=15, operating_system=OperatingSystem.UBUNTU),
43+
Laptop(id=4, manufacturer="Apple", model="macBook", screen_size_in_inches=13, operating_system=OperatingSystem.MACOS),
44+
]
45+
46+
for person in people:
47+
possible_laptops = find_possible_laptops(laptops, person)
48+
print(f"Possible laptops for {person.name}: {possible_laptops}")

0 commit comments

Comments
 (0)