-
-
Notifications
You must be signed in to change notification settings - Fork 21
ZA | 25-SDC-July | Luke Manyamazi | Sprint 4 | Prep Exercises #41
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
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,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}") |
| 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 | ||
| 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)) | ||
| 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) |
| 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" | ||
|
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. 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? |
||
| 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 | ||
|
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. 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) | ||
| 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}") |
| 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: | ||
|
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 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()}") | ||
| 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 |
| 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) |
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.
Could you suggest a change that would fix this error?