Skip to content

Commit 7e1a82b

Browse files
exercises
1 parent 8a9931b commit 7e1a82b

10 files changed

Lines changed: 335 additions & 0 deletions
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
def double(value): return value * 2
2+
3+
4+
5+
def fix_double(value):
6+
return int(value) * 2
7+
8+
print(double("22"))
9+
print(fix_double("22"))
10+
11+
#it returns "2222" -|> * works with string and it repeats it but "/" does not work with string
12+
# fix_double returns 44 becouse we converted the string to number
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
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+
def is_adult(person: Person) -> bool:
9+
return person.age >= 18
10+
11+
def print_email(person: Person) -> None:
12+
return person.email
13+
14+
imran = Person("Imran", 22, "Ubuntu")
15+
print(imran.name)
16+
#print(imran.address) doesnt worrk becuase there no address atribute
17+
18+
eliza = Person("Eliza", 34, "Arch Linux")
19+
print(eliza.name)
20+
#print(eliza.address) doesnt worrk becuase there no address atribute
21+
22+
print(is_adult(imran))
23+
24+
#print(print_email(imran)) there is no email atribute
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+
@dataclass(frozen=True)
5+
class Person:
6+
name: str
7+
dob: date
8+
preferred_operating_system: str
9+
10+
def is_adult(self) -> bool:
11+
today = date.today()
12+
13+
age = today.year - self.dob.year
14+
15+
# adjust if birthday hasn’t happened yet this year
16+
if (today.month, today.day) < (self.dob.month, self.dob.day):
17+
age -= 1
18+
19+
return age >= 18
20+
21+
22+
imran = Person("Imran", date(2003, 5, 10), "Ubuntu")
23+
24+
print(imran)
25+
print(imran.is_adult())

sprint5-prep-exercises/double.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
def double(number):
2+
return number * 3
3+
4+
def fix_double(number):
5+
return number * 2
6+
7+
print(double(10))
8+
print(fix_double(10))
9+
10+
#it wants double so we multiply by 2

sprint5-prep-exercises/enums.py

Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
from dataclasses import dataclass
2+
from enum import Enum
3+
from typing import List
4+
import sys
5+
6+
class OperatingSystem(Enum):
7+
MACOS = "macOS"
8+
ARCH = "Arch Linux"
9+
UBUNTU = "Ubuntu"
10+
11+
@dataclass(frozen=True)
12+
class Person:
13+
name: str
14+
age: int
15+
preferred_operating_system: OperatingSystem
16+
17+
18+
@dataclass(frozen=True)
19+
class Laptop:
20+
id: int
21+
manufacturer: str
22+
model: str
23+
screen_size_in_inches: float
24+
operating_system: OperatingSystem
25+
26+
27+
def find_possible_laptops(laptops: List[Laptop], person: Person) -> List[Laptop]:
28+
possible_laptops = []
29+
for laptop in laptops:
30+
if laptop.operating_system == person.preferred_operating_system:
31+
possible_laptops.append(laptop)
32+
return possible_laptops
33+
34+
35+
36+
laptops = [
37+
Laptop(id=1, manufacturer="Dell", model="XPS", screen_size_in_inches=13, operating_system=OperatingSystem.ARCH),
38+
Laptop(id=2, manufacturer="Dell", model="XPS", screen_size_in_inches=15, operating_system=OperatingSystem.UBUNTU),
39+
Laptop(id=3, manufacturer="Dell", model="XPS", screen_size_in_inches=15, operating_system=OperatingSystem.UBUNTU),
40+
Laptop(id=4, manufacturer="Apple", model="macBook", screen_size_in_inches=13, operating_system=OperatingSystem.MACOS),
41+
]
42+
43+
44+
name = input("Enter your name: ")
45+
46+
try:
47+
age = int(input("Enter your age: "))
48+
except ValueError:
49+
print("Invalid age. Must be number!", file=sys.stderr)
50+
sys.exit(1)
51+
52+
os_input = input("Enter preferred OS (macOS / Arch Linux / Ubuntu): ")
53+
54+
try:
55+
preferred_os = OperatingSystem(os_input)
56+
except ValueError:
57+
print("Invalid operating system.", file=sys.stderr)
58+
sys.exit(1)
59+
60+
person = Person(name=name, age=age, preferred_operating_system=preferred_os)
61+
62+
matching = find_possible_laptops(laptops, person)
63+
64+
print(f"\nWe have {len(matching)} laptop(s) with {person.preferred_operating_system.value}.")
65+
66+
# Compare with other OS availability
67+
os_counts = {}
68+
69+
for laptop in laptops:
70+
os_counts[laptop.operating_system] = os_counts.get(laptop.operating_system, 0) + 1
71+
72+
best_os = max(os_counts, key=os_counts.get)
73+
74+
if best_os != person.preferred_operating_system:
75+
print(
76+
f"If you're flexible, {best_os.value} has more laptops available "
77+
f"({os_counts[best_os]} total)."
78+
)

sprint5-prep-exercises/generics.py

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
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+
children: List["Person"]
9+
10+
fatma = Person(name="Fatma", age=10, children=[])
11+
aisha = Person(name="Aisha", age=12, children=[])
12+
13+
imran = Person(name="Imran", age=50, children=[fatma, aisha])
14+
15+
def print_family_tree(person: Person) -> None:
16+
print(person.name)
17+
for child in person.children:
18+
print(f"- {child.name} ({child.age})")
19+
20+
print_family_tree(imran)
Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
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+
27+
28+
# create Child object
29+
person1 = Child("Elizaveta", "Alekseeva")
30+
# expected state:
31+
# first_name = Elizaveta
32+
# last_name = Alekseeva
33+
# previous_last_names = []
34+
35+
print(person1.get_name())
36+
# exxpected: "Elizaveta Alekseeva"
37+
38+
print(person1.get_full_name())
39+
# expected: "Elizaveta Alekseeva"
40+
# (no previous last names yet)
41+
42+
person1.change_last_name("Tyurina")
43+
# Expected state update:
44+
# previous_last_names = ["Alekseeva"]
45+
# last_name = "Tyurina"
46+
47+
print(person1.get_name())
48+
# expected: "Elizaveta Tyurina"
49+
50+
print(person1.get_full_name())
51+
# expected: "Elizaveta Tyurina (née Alekseeva)"
52+
53+
54+
55+
# Parent object
56+
57+
person2 = Parent("Elizaveta", "Alekseeva")
58+
59+
print(person2.get_name())
60+
# expected: "Elizaveta Alekseeva"
61+
62+
print(person2.get_full_name())
63+
# error: Parent has no method get_full_name
64+
65+
# program stops here
66+
67+
# these lines will NOT run:
68+
# person2.change_last_name("Tyurina") # AttributeError
69+
# print(person2.get_name())
70+
# print(person2.get_full_name())

sprint5-prep-exercises/methods.py

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
2+
3+
from datetime import date
4+
5+
class Person:
6+
def __init__(self, name: str, dob: date, preferred_operating_system: str):
7+
self.name = name
8+
self.dob = dob
9+
self.preferred_operating_system = preferred_operating_system
10+
11+
def is_adult(self) -> bool:
12+
today = date.today()
13+
14+
age = today.year - self.dob.year
15+
16+
# adjust if birthday hasn’t happened yet this year
17+
if (today.month, today.day) < (self.dob.month, self.dob.day):
18+
age -= 1
19+
20+
return age >= 18
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
from typing import Dict
2+
3+
def open_account(balances: Dict[str, int], name: str, amount: int) -> None:
4+
balances[name] = amount
5+
6+
def sum_balances(accounts: Dict[str, int]) -> int:
7+
total = 0
8+
for name, pence in accounts.items():
9+
print(f"{name} had balance {pence}")
10+
total += pence
11+
return total
12+
13+
def format_pence_as_string(total_pence: int) -> str:
14+
if total_pence < 100:
15+
return f"{total_pence}p"
16+
pounds = total_pence // 100
17+
pence = total_pence % 100
18+
return f"£{pounds}.{pence:02d}"
19+
20+
21+
balances: Dict[str, int] = {
22+
"Sima": 700,
23+
"Linn": 545,
24+
"Georg": 831,
25+
}
26+
27+
# ✅ correct calls
28+
open_account(balances, "Tobi", 913)
29+
open_account(balances, "Olya", 713)
30+
31+
total_pence = sum_balances(balances)
32+
total_string = format_pence_as_string(total_pence)
33+
34+
print(f"The bank accounts total {total_string}")
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 = []
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(1, "Dell", "XPS", 13, "Arch Linux"),
35+
Laptop(2, "Dell", "XPS", 15, "Ubuntu"),
36+
Laptop(3, "Dell", "XPS", 15, "ubuntu"),
37+
Laptop(4, "Apple", "macBook", 13, "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}")

0 commit comments

Comments
 (0)