Skip to content

Commit bbf12ef

Browse files
committed
refactor
1 parent 220bf3a commit bbf12ef

1 file changed

Lines changed: 53 additions & 0 deletions

File tree

type/refactor.py

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

0 commit comments

Comments
 (0)