Skip to content
Closed
Show file tree
Hide file tree
Changes from 3 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
Binary file not shown.
30 changes: 30 additions & 0 deletions prep exercises/bank_account.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
def open_account(balances, name, amount):

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Part of the bank account task involved making type annotations, can you try that?

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

I have just added the type annotations on the functions

balances[name] = amount

def sum_balances(accounts):
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):
if total_pence < 100:
return f"{total_pence}p"
pounds = int(total_pence / 100)
pence = total_pence % 100
return f"£{pounds}.{pence:02d}"

balances = {
"Sima": 700,
"Linn": 545,
"Georg": 831,
}

open_account(balances, "Tobi", 9.13)
open_account(balances, "Olya", "£7.13")

total_pence = sum_balances(balances)
total_string = format_pence_as_string(total_pence)

print(f"The bank accounts total {total_string}")
20 changes: 20 additions & 0 deletions prep exercises/dataclasses_exercise.py

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

did you try running the code and spotting what the error was?

Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
from dataclasses import dataclass
from datetime import date

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

def is_adult(self):
today_date = date.today()
birth_date = date.fromisoformat(self.date_of_birth)
age = today_date.year - birth_date.year

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Would this date calculation work all the time? What about if they are one day away from a 18th birthday in the current year?

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

This calculation will not work in this scenario because I am only comparing the years. I have changed the functionality to include the days instead of only comparing the years


return age >= 18


imran = Person("Imran", "2019-10-18", "Ubuntu")

print(imran.is_adult())
22 changes: 22 additions & 0 deletions prep exercises/generic_exercise.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
from dataclasses import dataclass
from typing import List

@dataclass(frozen=True)

class Person:
name: str
children: List["Person"]
age: int

Copy link
Copy Markdown
Contributor

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?

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

I have changed the age to date_of_birth and used the function i used previous to calculate the age of the person as long as the birthday is given.


fatma = Person(name="Fatma", children=[], age=25)
aisha = Person(name="Aisha", children=[], age = 15)

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

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)
18 changes: 18 additions & 0 deletions prep exercises/objects_and_classes.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
from datetime import date

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

def is_adult(self):

Copy link
Copy Markdown
Contributor

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 the function this way?

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

Writing a method has many benefits, such as it helps in encapsulating logic, as a result, it makes the method reusable in other instances of the class. The problem of these functions is that they become difficult to test in isolation as compared to free functions.

today_date = date.today()

return (today_date.year - self.date_of_birth.year) >= 18


imran = Person("Imran", "2005-12-04", "Ubunut", "Wardend Road, Birmingham")

print(imran.is_adult())
5 changes: 5 additions & 0 deletions prep exercises/test.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
def double(n):
return n * 3

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Does this function actually double? What change could you make to resolve the issue?

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

I think for this function, there are two possible interpretations. Firstly, the function might have been meant to return the triple value of n. In this case, I could the function name from double(n) to triple(n). Secondly, the function might actually been meant to return double the value of n. Meaning I can just change the 3 to 2.


num = double(21)
print(num)
Loading