-
-
Notifications
You must be signed in to change notification settings - Fork 21
West Midlands | July SDC | Gabriel Deng | Sprint 4 | Prep exercises #39
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
d7b5b7a
c172120
8e2f3ce
63968ad
9b3ad1e
fe955b3
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,32 @@ | ||
| from typing import Dict | ||
|
|
||
| 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 = int(total_pence / 100) | ||
| pence = total_pence % 100 | ||
| return f"£{pounds}.{pence:02d}" | ||
|
|
||
| balances = { | ||
| "Sima": 700, | ||
| "Linn": 545, | ||
| "Georg": 831, | ||
| } | ||
|
|
||
| open_account(balances, "Tobi", 913) | ||
| open_account(balances, "Olya", 713) | ||
|
|
||
| total_pence = sum_balances(balances) | ||
| total_string = format_pence_as_string(total_pence) | ||
|
|
||
| print(f"The bank accounts total {total_string}") |
| 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: 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 | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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?
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 |
||
|
|
||
| if (today_date.month, today_date.day) < (birth_date.month, birth_date.day): | ||
| age -= 1 | ||
|
|
||
| return age >= 18 | ||
|
|
||
|
|
||
| imran = Person("Imran", "2007-11-02", "Ubuntu") | ||
|
|
||
| print(imran.is_adult()) | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,33 @@ | ||
| from dataclasses import dataclass | ||
| from typing import List | ||
| from datetime import date | ||
|
|
||
| @dataclass(frozen=True) | ||
|
|
||
| class Person: | ||
| name: str | ||
| children: List["Person"] | ||
| date_of_birth: str | ||
|
|
||
| def person_age(self) -> int: | ||
| today_date = date.today() | ||
| birth_date = date.fromisoformat(self.date_of_birth) | ||
| age = today_date.year - birth_date.year | ||
|
|
||
| if (today_date.month, today_date.day) < (birth_date.month, birth_date.day): | ||
| age -= 1 | ||
| return age | ||
|
|
||
|
|
||
| fatma = Person(name="Fatma", children=[], date_of_birth="2010-06-15") | ||
| aisha = Person(name="Aisha", children=[], date_of_birth="2012-09-20") | ||
|
|
||
| imran = Person(name="Imran", children=[fatma, aisha], date_of_birth="1998-10-16") | ||
|
|
||
| def print_family_tree(person: Person) -> None: | ||
| print(f"{person.name} (Age: {person.person_age()})") | ||
|
|
||
| for child in person.children: | ||
| print(f"- {child.name} ({child.person_age()})") | ||
|
|
||
| print_family_tree(imran) |
| 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): | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. What is a benefit or drawback of writing the function this way?
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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()) | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,5 @@ | ||
| def triple(n): | ||
| return n * 3 | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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?
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 = triple(21) | ||
| print(num) | ||
There was a problem hiding this comment.
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?