Skip to content

Commit 87f4255

Browse files
added prep exercises
1 parent 16ace89 commit 87f4255

9 files changed

Lines changed: 188 additions & 0 deletions

File tree

Prep Exercises/bank.py

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+
# baances is a Dict mapping account names(str) to balances (int)
4+
def open_account(balances: Dict[str, int], name: str, amount: int) -> None:
5+
balances[name] = amount
6+
7+
def sum_balances(accounts: Dict[str, int]) -> 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_string(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+
22+
balances: Dict[str, int] = {
23+
"Sima": 700,
24+
"Linn": 545,
25+
"Georg": 831,
26+
}
27+
28+
open_account(balances, "Tobi", 913)
29+
open_account(balances, "Olya", 713)
30+
31+
total_pence: int = sum_balances(balances)
32+
total_string: str = format_pence_as_string(total_pence)
33+
34+
print(f"The bank accounts total {total_string}")

Prep Exercises/classes.py

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+
def is_adult(person: Person) -> bool:
8+
return person.age >= 18
9+
10+
def location(person: Person) -> str:
11+
return person.location
12+
13+
imran = Person("Imran", 22, "Ubuntu")
14+
print(imran.name)
15+
# print(imran.address) # Person has no address attribute
16+
print(imran.age)
17+
print(is_adult(imran))
18+
print(location(imran))
19+
20+
eliza = Person("Eliza", 34, "Arch Linux")
21+
print(eliza.name)
22+
# print(eliza.address) # Person has no address attribute
23+
print(eliza.age)
24+
print(is_adult(eliza))

Prep Exercises/dataclass.py

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

Prep Exercises/double22.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
def double(value):
2+
return value * 2
3+
4+
print(double("22"))
5+
6+
# I somehow expected it to return this answer, because the string "22" doubled is "2222"

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+
children: List["Person"]
8+
age: int
9+
10+
fatma = Person(name="Fatma", age=6, children=[])
11+
aisha = Person(name="Aisha", age=13, children=[])
12+
13+
imran = Person(name="Imran", age=34, 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)

Prep Exercises/laptops.py

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(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/methods.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+
3+
class Person:
4+
def __init__(self, name: str, date_of_birth: date, preferred_operating_system: str):
5+
self.name = name
6+
self.date_of_birth = date_of_birth
7+
self.preferred_operating_system = preferred_operating_system
8+
9+
@property
10+
def age(self) -> int:
11+
today = date.today()
12+
return today.year - self.date_of_birth.year - (
13+
(today.month, today.day) < (self.date_of_birth.month, self.date_of_birth.day)
14+
)
15+
16+
def is_adult(self) -> bool:
17+
return self.age >= 18
18+
19+
imran = Person(
20+
name="Imran",
21+
date_of_birth=date(2003, 7, 14),
22+
preferred_operating_system="Ubuntu"
23+
)
24+
25+
print(f"{imran.name} is {imran.age} years old.")
26+
print(f"Is {imran.name} an adult? {imran.is_adult()}")

Prep Exercises/typeLimits.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
def double(number):
2+
return number * 3
3+
4+
print(double(10))
5+
6+
# I expected it to return 20, because I thought it would double the number
7+
# but it actually triples it to return 30
8+
# Fix - change the multiplication factor from 3 to 2

test.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
def double(n: int) -> int:
2+
return n * 2
3+
4+
num = double(21)
5+
print(num)

0 commit comments

Comments
 (0)