Skip to content

Commit 1510d2b

Browse files
committed
moving files from the main directory to a separate folder
1 parent 75f4c5b commit 1510d2b

6 files changed

Lines changed: 278 additions & 0 deletions

File tree

prep-exercises/dataclasses_ex.py

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

prep-exercises/enums.py

Lines changed: 113 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,113 @@
1+
from dataclasses import dataclass
2+
from enum import Enum
3+
from typing import List, Dict
4+
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("There is an operating system that has more laptops available.If you’re willing to accept them, there is a list:")
60+
list_os = []
61+
for os, count in alternative_laptops.items():
62+
list_os.append(f"{os.value}: {count}")
63+
print(" ,".join(list_os))
64+
return alternative_laptops
65+
else:
66+
print("There is not an operating system that has more laptops available.")
67+
return alternative_laptops
68+
69+
while True:
70+
user_name = input("Type your name: ").strip()
71+
if len(user_name) < 3:
72+
print(f"Error, {user_name} is not valid. Try again, length should be more than 3 characters.")
73+
continue
74+
break
75+
76+
while True:
77+
user_age = input("Type your age: ").strip()
78+
try:
79+
user_age_int = int(user_age)
80+
if user_age_int < 18:
81+
raise ValueError
82+
break
83+
except ValueError:
84+
print("Invalid age, try again! Borrowing allowed from 18 years old.")
85+
86+
available_os = [os.value for os in OperatingSystem]
87+
print("Available OSs are: ", ",".join(available_os))
88+
while True:
89+
user_operating_system = input("Type operating system: ").strip()
90+
try:
91+
if user_operating_system not in available_os:
92+
raise ValueError
93+
break
94+
except ValueError:
95+
print(f"Error, {user_operating_system} is not in available list\n"
96+
f"Available OSs are: {','.join(available_os)}. Try again!.", file=sys.stderr)
97+
98+
99+
preferred_operating_system = OperatingSystem(user_operating_system)
100+
101+
user = Person(name=user_name, age=user_age_int, preferred_operating_system=preferred_operating_system)
102+
103+
104+
laptops = [
105+
Laptop(id=1, manufacturer="Dell", model="XPS", screen_size_in_inches=13, operating_system=OperatingSystem.ARCH),
106+
Laptop(id=2, manufacturer="Dell", model="XPS", screen_size_in_inches=15, operating_system=OperatingSystem.UBUNTU),
107+
Laptop(id=3, manufacturer="Dell", model="XPS", screen_size_in_inches=15, operating_system=OperatingSystem.UBUNTU),
108+
Laptop(id=4, manufacturer="Apple", model="macBook", screen_size_in_inches=13, operating_system=OperatingSystem.MACOS),
109+
]
110+
111+
possible_laptops = count_possible_laptops(laptops, user)
112+
print(f"Possible laptops for {user_name}: {possible_laptops}")
113+
alternative_laptops = chose_alternative_laptops(laptops, user)

prep-exercises/familytree.py

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
from dataclasses import dataclass
2+
from typing import List
3+
4+
@dataclass(frozen=True)
5+
class Person:
6+
name: str
7+
children: List["Person"]
8+
age: int
9+
10+
fatma = Person(name="Fatma", children=[], age=17)
11+
aisha = Person(name="Aisha", children=[], age=25)
12+
13+
imran = Person(name="Imran", children=[fatma, aisha], age=51)
14+
15+
def print_family_tree(person: Person) -> None:
16+
print(person.name, f"({person.age})")
17+
for child in person.children:
18+
print(f"- {child.name} ({child.age})")
19+
20+
# recursion
21+
22+
def print_family_tree_recursion(person: Person, level:str = "¬") -> None:
23+
print(f"{level} {person.name} ({person.age})")
24+
for child in person.children:
25+
print_family_tree_recursion(child, level + "|-")
26+
27+
print_family_tree_recursion(fatma)

prep-exercises/inherit.py

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
from typing import List
2+
3+
class Parent:
4+
def __init__(self, first_name: str, last_name: str):
5+
self.first_name = first_name
6+
self.last_name = last_name
7+
8+
def get_name(self) -> str:
9+
return f"{self.first_name} {self.last_name}"
10+
11+
12+
class Child(Parent):
13+
def __init__(self, first_name: str, last_name: str):
14+
super().__init__(first_name, last_name)
15+
self.previous_last_names: List [str]= []
16+
17+
def change_last_name(self, last_name: str) -> None:
18+
self.previous_last_names.append(self.last_name)
19+
self.last_name = last_name
20+
21+
def get_full_name(self) -> str:
22+
suffix: str = ""
23+
if len(self.previous_last_names) > 0:
24+
suffix = f" (née {self.previous_last_names[0]})"
25+
return f"{self.first_name} {self.last_name}{suffix}"
26+
27+
person1 = Child("Elizaveta", "Alekseeva")
28+
print(person1.get_name())
29+
print(person1.get_full_name())
30+
person1.change_last_name("Tyurina")
31+
print(person1.get_name())
32+
print(person1.get_full_name())
33+
34+
person2 = Parent("Elizaveta", "Alekseeva")
35+
print(person2.get_name())
36+
# print(person2.get_full_name()) // this method dose not belong to Parent class
37+
# person2.change_last_name("Tyurina") // this method dose not belong to Parent class
38+
print(person2.get_name())
39+
# print(person2.get_full_name()) // this method dose not belong to Parent class
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
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: list[Laptop] = []
22+
for laptop in laptops:
23+
if laptop.operating_system in person.preferred_operating_systems:
24+
possible_laptops.append(laptop)
25+
return possible_laptops
26+
27+
28+
people = [
29+
Person(name="Imran", age=22, preferred_operating_systems=["Ubuntu"]),
30+
Person(name="Eliza", age=34, preferred_operating_systems=["Arch Linux"]),
31+
]
32+
33+
laptops = [
34+
Laptop(id=1, manufacturer="Dell", model="XPS", screen_size_in_inches=13, operating_system="Arch Linux"),
35+
Laptop(id=2, manufacturer="Dell", model="XPS", screen_size_in_inches=15, operating_system="Ubuntu"),
36+
Laptop(id=3, manufacturer="Dell", model="XPS", screen_size_in_inches=15, operating_system="ubuntu"),
37+
Laptop(id=4, manufacturer="Apple", model="macBook", screen_size_in_inches=13, operating_system="macOS"),
38+
]
39+
40+
for person in people:
41+
possible_laptops = find_possible_laptops(laptops, person)
42+
print(f"Possible laptops for {person.name}: {possible_laptops}")

prep-exercises/typesExer.py

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
from typing import Union, Dict
2+
3+
4+
def open_account(balances: Dict[str, int], name: str, amount: Union[str, float]):
5+
balances[name] = int(float(amount) *100)
6+
7+
def sum_balances(accounts: Dict[str, int]):
8+
total = 0
9+
for name, pence in accounts.items():
10+
print(f"{name} had balance {pence}")
11+
total += pence
12+
return total
13+
14+
def format_pence_as_pound(total_pence: int) -> str:
15+
if total_pence < 100:
16+
return f"{total_pence}p"
17+
pounds = total_pence // 100
18+
pence = total_pence % 100
19+
return f"£{pounds}.{pence:02d}"
20+
21+
balances = {
22+
"Sima": 700,
23+
"Linn": 545,
24+
"Georg": 831,
25+
}
26+
27+
open_account(balances, "Tobi", 9.13)
28+
open_account(balances, "Olya", 7.13)
29+
30+
total_pence = sum_balances(balances)
31+
total_pound = format_pence_as_pound(total_pence)
32+
33+
print(f"The bank accounts total {total_pound}")

0 commit comments

Comments
 (0)