Skip to content

Commit 370025e

Browse files
committed
dataclasses and laptopAllocation exercises
1 parent 8ddccac commit 370025e

2 files changed

Lines changed: 68 additions & 0 deletions

File tree

prep-exercises/dataclasses.py

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
from dataclasses import dataclass
2+
from datetime import date
3+
4+
5+
@dataclass
6+
class Person:
7+
name: str
8+
date_of_birth: date
9+
preferred_operating_system: str
10+
11+
def is_adult(self):
12+
today = date.today()
13+
14+
age = today.year - self.date_of_birth.year
15+
16+
if (today.month, today.day) < (self.date_of_birth.month, self.date_of_birth.day):
17+
age -= 1
18+
19+
return age >= 18
20+
21+
22+
imran = Person("Imran", date(2002, 5, 10), "Ubuntu")
23+
24+
print(imran)
25+
print(imran.is_adult())

prep-exercises/laptopAllocated.py

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
from dataclasses import dataclass
2+
from typing import List
3+
from enum import Enum
4+
5+
6+
class OperatingSystem(Enum):
7+
UBUNTU = "Ubuntu"
8+
MAC = "Mac"
9+
WINDOWS = "Windows"
10+
11+
12+
@dataclass
13+
class Person:
14+
name: str
15+
age: int
16+
preferred_os: OperatingSystem
17+
18+
19+
@dataclass
20+
class Laptop:
21+
model: str
22+
os: OperatingSystem
23+
24+
25+
def get_matching_laptops(laptops: List[Laptop], person: Person) -> List[Laptop]:
26+
matches = []
27+
28+
for laptop in laptops:
29+
if laptop.os == person.preferred_os:
30+
matches.append(laptop)
31+
32+
return matches
33+
34+
35+
laptops = [
36+
Laptop("Dell", OperatingSystem.UBUNTU),
37+
Laptop("MacBook", OperatingSystem.MAC),
38+
Laptop("HP", OperatingSystem.WINDOWS),
39+
]
40+
41+
person = Person("Ali", 22, OperatingSystem.UBUNTU)
42+
43+
print(get_matching_laptops(laptops, person))

0 commit comments

Comments
 (0)