Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 34 additions & 0 deletions Prep Exercises/bank.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
from typing import Dict

# balances is a Dict mapping account names(str) to balances (int)
def open_account(balances: Dict[str, int], name: str, amount: int) -> None:
balances[name] = amount

def sum_balances(accounts: Dict[str, int]) -> int:
total = 0
for name, pence in accounts.items():
print(f"{name} had balance {pence}")
total += pence
return total

def format_pence_as_string(total_pence: int) -> str:
if total_pence < 100:
return f"{total_pence}p"
pounds = total_pence // 100
pence = total_pence % 100
return f"£{pounds}.{pence:02d}"


balances: Dict[str, int] = {
"Sima": 700,
"Linn": 545,
"George": 831,
}

open_account(balances, "Tobi", 913)
open_account(balances, "Ola", 713)

total_pence: int = sum_balances(balances)
total_string: str = format_pence_as_string(total_pence)

print(f"The bank accounts total {total_string}")
24 changes: 24 additions & 0 deletions Prep Exercises/classes.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
class Person:
def __init__(self, name: str, age: int, preferred_operating_system: str):
self.name = name
self.age = age
self.preferred_operating_system = preferred_operating_system

def is_adult(person: Person) -> bool:
return person.age >= 18

def location(person: Person) -> str:
return person.location

imran = Person("Imran", 22, "Ubuntu")
print(imran.name)
# print(imran.address) # Person has no address attribute

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could you suggest a change that would fix this error?

print(imran.age)
print(is_adult(imran))
print(location(imran))

eliza = Person("Eliza", 34, "Arch Linux")
print(eliza.name)
# print(eliza.address) # Person has no address attribute
print(eliza.age)
print(is_adult(eliza))
23 changes: 23 additions & 0 deletions Prep Exercises/dataclass.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
from dataclasses import dataclass
from datetime import date

@dataclass(frozen=True)
class Person:
name: str
date_of_birth: date
preferred_operating_system: str

def is_adult(self) -> bool:
today = date.today()
age = today.year - self.date_of_birth.year - (
(today.month, today.day) < (self.date_of_birth.month, self.date_of_birth.day)
)
return age >= 18

imran = Person(name="Imran", date_of_birth=date(2003, 7, 14), preferred_operating_system="Ubuntu")
print(imran)
print(imran.is_adult())

# Equality check
imran2 = Person(name="Imran", date_of_birth=date(2003, 7, 14), preferred_operating_system="Ubuntu")
print(imran == imran2)
6 changes: 6 additions & 0 deletions Prep Exercises/double22.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
def double(value):
return value * 2

print(double("22"))

# I somehow expected it to return this answer, because the string "22" doubled is "2222"

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this behaviour consistent for all inputs (think about possible types of input). Are there any changes you would suggest making to prevent bugs from happening if this function were used incorrectly?

20 changes: 20 additions & 0 deletions Prep Exercises/generics.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
from dataclasses import dataclass
from typing import List

@dataclass(frozen=True)
class Person:
name: str
children: List["Person"]
age: int

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you solve this in a way that it always prints the current age, rather than hard-coding a specific age?


fatma = Person(name="Fatma", age=6, children=[])
aisha = Person(name="Aisha", age=13, children=[])

imran = Person(name="Imran", age=34, children=[fatma, aisha])

def print_family_tree(person: Person) -> None:
print(person.name)
for child in person.children:
print(f"- {child.name} ({child.age})")

print_family_tree(imran)
42 changes: 42 additions & 0 deletions Prep Exercises/laptops.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
from dataclasses import dataclass
from typing import List

@dataclass(frozen=True)
class Person:
name: str
age: int
preferred_operating_systems: List[str]


@dataclass(frozen=True)
class Laptop:
id: int
manufacturer: str
model: str
screen_size_in_inches: float
operating_system: str


def find_possible_laptops(laptops: List[Laptop], person: Person) -> List[Laptop]:
possible_laptops = []
for laptop in laptops:
if laptop.operating_system in person.preferred_operating_systems:
possible_laptops.append(laptop)
return possible_laptops


people = [
Person(name="Imran", age=22, preferred_operating_systems=["Ubuntu"]),
Person(name="Eliza", age=34, preferred_operating_systems=["Arch Linux"]),
]

laptops = [
Laptop(id=1, manufacturer="Dell", model="XPS", screen_size_in_inches=13, operating_system="Arch Linux"),
Laptop(id=2, manufacturer="Dell", model="XPS", screen_size_in_inches=15, operating_system="Ubuntu"),
Laptop(id=3, manufacturer="Dell", model="XPS", screen_size_in_inches=15, operating_system="ubuntu"),
Laptop(id=4, manufacturer="Apple", model="macBook", screen_size_in_inches=13, operating_system="macOS"),
]

for person in people:
possible_laptops = find_possible_laptops(laptops, person)
print(f"Possible laptops for {person.name}: {possible_laptops}")
26 changes: 26 additions & 0 deletions Prep Exercises/methods.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
from datetime import date

class Person:
def __init__(self, name: str, date_of_birth: date, preferred_operating_system: str):
self.name = name
self.date_of_birth = date_of_birth
self.preferred_operating_system = preferred_operating_system

@property
def age(self) -> int:

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What is a benefit or drawback of writing a function this way?

today = date.today()
return today.year - self.date_of_birth.year - (
(today.month, today.day) < (self.date_of_birth.month, self.date_of_birth.day)
)

def is_adult(self) -> bool:
return self.age >= 18

imran = Person(
name="Imran",
date_of_birth=date(2003, 7, 14),
preferred_operating_system="Ubuntu"
)

print(f"{imran.name} is {imran.age} years old.")
print(f"Is {imran.name} an adult? {imran.is_adult()}")
8 changes: 8 additions & 0 deletions Prep Exercises/typeLimits.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
def double(number):
return number * 3

print(double(10))

# I expected it to return 20, because I thought it would double the number
# but it actually triples it to return 30
# Fix - change the multiplication factor from 3 to 2
5 changes: 5 additions & 0 deletions test.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
def double(n: int) -> int:
return n * 2

num = double(21)
print(num)