Skip to content

Commit f873115

Browse files
committed
done exercises
1 parent 16ace89 commit f873115

11 files changed

Lines changed: 402 additions & 0 deletions

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
.venv
2+
node_modules

bank_accounts.py

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

classes_and_objects.py

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
class Person:
2+
def __init__(self, name: str, age: int, preferred_operating_system: str):
3+
self.name = name
4+
self.age = age
5+
self.preferred_operating_system = preferred_operating_system
6+
7+
8+
imran = Person("Imran", 22, "Ubuntu")
9+
print(imran.name)
10+
# print(imran.address) address is not an attribute of the Person class
11+
12+
eliza = Person("Eliza", 34, "Arch Linux")
13+
print(eliza.name)
14+
# print(eliza.address)
15+
16+
17+
def is_adult(person: Person) -> bool:
18+
return person.age >= 18
19+
20+
21+
print(is_adult(imran))
22+
23+
24+
# Exercise:
25+
# Write a new function in the file that accepts a Person as a parameter and tries to access a property that
26+
# doesn’t exist. Run it through mypy and check that it does report an error.
27+
28+
29+
def live_in_london(person: Person) -> bool:
30+
return person.address == "London"
31+
32+
33+
live_in_london(imran)

dataclass.py

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

double.py

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
def half(value):
2+
return value / 2
3+
4+
5+
def double(value):
6+
return value * 2
7+
8+
9+
def second(value):
10+
return value[1]
11+
12+
13+
# ✍️exercise
14+
# Predict what double("22") will do. Then run the code and check. Did it do what you expected? Why did it return the value it did
15+
16+
# Answer
17+
# double "22" will return "2222" because when we multiply string, code write that string several times

enums.py

Lines changed: 137 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,137 @@
1+
from dataclasses import dataclass
2+
from typing import List, Tuple
3+
from enum import Enum
4+
import sys
5+
from collections import Counter
6+
7+
8+
class OperatingSystem(Enum):
9+
MACOS = "macOS"
10+
ARCH = "Arch Linux"
11+
UBUNTU = "Ubuntu"
12+
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+
laptops = [
31+
Laptop(
32+
id=1,
33+
manufacturer="Dell",
34+
model="XPS",
35+
screen_size_in_inches=13,
36+
operating_system=OperatingSystem.ARCH,
37+
),
38+
Laptop(
39+
id=2,
40+
manufacturer="Dell",
41+
model="XPS",
42+
screen_size_in_inches=15,
43+
operating_system=OperatingSystem.UBUNTU,
44+
),
45+
Laptop(
46+
id=3,
47+
manufacturer="Dell",
48+
model="XPS",
49+
screen_size_in_inches=15,
50+
operating_system=OperatingSystem.UBUNTU,
51+
),
52+
Laptop(
53+
id=4,
54+
manufacturer="Apple",
55+
model="macBook",
56+
screen_size_in_inches=13,
57+
operating_system=OperatingSystem.MACOS,
58+
),
59+
]
60+
61+
62+
users = []
63+
64+
65+
def find_possible_laptops(laptops: List[Laptop], person: Person) -> List[Laptop]:
66+
possible_laptops = []
67+
for laptop in laptops:
68+
if laptop.operating_system == person.preferred_operating_system:
69+
possible_laptops.append(laptop)
70+
return possible_laptops
71+
72+
73+
def input_validation() -> Tuple[str, int, OperatingSystem]:
74+
input_user_name = input("Enter your name: ")
75+
if not isinstance(input_user_name, str):
76+
sys.stderr.write("Name must be a string")
77+
sys.exit(1)
78+
79+
try:
80+
input_user_age = int(input("Enter your age: "))
81+
except ValueError:
82+
sys.stderr.write("Age must be an integer")
83+
sys.exit(1)
84+
85+
input_preferred_operating_system = input(
86+
"Enter your preferred operating system (macOS/Arch Linux/Ubuntu): "
87+
)
88+
89+
try:
90+
preferred_os = OperatingSystem(input_preferred_operating_system)
91+
except ValueError:
92+
sys.stderr.write("Wrong OS")
93+
sys.exit(1)
94+
95+
return input_user_name, input_user_age, preferred_os
96+
97+
98+
def count_operating_systems(laptops: List[Laptop]) -> Counter[OperatingSystem]:
99+
return Counter(laptop.operating_system for laptop in laptops)
100+
101+
102+
def recommend_os(user: Person, laptops: List[Laptop]) -> None:
103+
os_counts = count_operating_systems(laptops)
104+
most_common_os, most_common_count = os_counts.most_common(1)[0]
105+
106+
user_count = os_counts[user.preferred_operating_system]
107+
108+
if (
109+
most_common_os != user.preferred_operating_system
110+
and user_count < most_common_count
111+
):
112+
print(f"More laptops are available with {most_common_os.value}.")
113+
114+
115+
def laptop() -> None:
116+
input_user_name, input_user_age, input_preferred_operating_system = (
117+
input_validation()
118+
)
119+
120+
users.append(
121+
Person(
122+
name=input_user_name,
123+
age=input_user_age,
124+
preferred_operating_system=OperatingSystem(
125+
input_preferred_operating_system
126+
),
127+
),
128+
)
129+
for user in users:
130+
possible_laptops = find_possible_laptops(laptops, user)
131+
print(
132+
f"Possible laptops for {user.name}:\n{'\n'.join(map(str, possible_laptops))}",
133+
)
134+
recommend_os(user, laptops)
135+
136+
137+
laptop()

generics.py

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

inheritance.py

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
class Parent:
2+
def __init__(self, first_name: str, last_name: str):
3+
self.first_name = first_name
4+
self.last_name = last_name
5+
6+
def get_name(self) -> str:
7+
return f"{self.first_name} {self.last_name}"
8+
9+
10+
class Child(Parent):
11+
def __init__(self, first_name: str, last_name: str):
12+
super().__init__(first_name, last_name)
13+
self.previous_last_names = []
14+
15+
def change_last_name(self, last_name) -> None:
16+
self.previous_last_names.append(self.last_name)
17+
self.last_name = last_name
18+
19+
def get_full_name(self) -> str:
20+
suffix = ""
21+
if len(self.previous_last_names) > 0:
22+
suffix = f" (née {self.previous_last_names[0]})"
23+
return f"{self.first_name} {self.last_name}{suffix}"
24+
25+
26+
person1 = Child("Elizaveta", "Alekseeva")
27+
print(person1.get_name())
28+
print(person1.get_full_name())
29+
person1.change_last_name("Tyurina")
30+
print(person1.get_name())
31+
print(person1.get_full_name())
32+
33+
person2 = Parent("Elizaveta", "Alekseeva")
34+
print(person2.get_name())
35+
# print(person2.get_full_name())
36+
# person2.change_last_name("Tyurina")
37+
print(person2.get_name())
38+
# print(person2.get_full_name())
39+
# Commented out these prints because the Parent class doesn't have these methods, which causes an "AttributeError"

is_adult.py

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

requirements.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
mypy

0 commit comments

Comments
 (0)