-
-
Notifications
You must be signed in to change notification settings - Fork 42
London | 25-SDC-Nov | Jesus del Moral | Sprint 5| Prep Exercises #295
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
c02301b
28986eb
5e2a021
d4d96c8
2518525
c151c3d
c2d51e7
294b028
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,38 @@ | ||
| # EXERCISE 1: Think of the advantages of using methods instead of free functions. Write them down in your notebook | ||
| # - Methods keep data and behavior together | ||
| # - Methods make code easier to read | ||
| # - Methods reduce mistakes | ||
| # - Easier to extend and maintain | ||
| # - Methods support inheritance | ||
| # - Better organization | ||
| # - Better organization | ||
| # - Methods enable polymorphism | ||
|
|
||
|
|
||
|
|
||
| # EXERCISE 2: Change the Person class to take a date of birth (using the standard library’s datetime.date class) and store it in a field instead of age. | ||
| # Update the is_adult method to act the same as before. | ||
|
|
||
|
|
||
| from datetime import date | ||
| from dataclasses import dataclass | ||
|
|
||
| @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 | ||
|
|
||
| # Adjust if birthday hasn't happened yet this year | ||
| if (today.month, today.day) < (self.date_of_birth.month, self.date_of_birth.day): | ||
| age -= 1 | ||
| return age >= 18 | ||
|
|
||
| imran = Person("Jesus", date(1980, 1, 12), "Ubuntu") | ||
| print(imran.is_adult()) | ||
|
|
||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,48 @@ | ||
| #Read the error, and make sure you understand what it’s telling you. | ||
|
|
||
|
|
||
|
|
||
| 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 | ||
|
|
||
|
|
||
|
|
||
| imran = Person("Imran", 22, "Ubuntu") | ||
| print(imran.name) | ||
|
|
||
|
|
||
| eliza = Person("Eliza", 34, "Arch Linux") | ||
| print(eliza.name) | ||
|
|
||
| def is_adult(person: Person) -> bool: | ||
| return person.age >= 18 | ||
|
|
||
| print(is_adult(imran)) | ||
|
|
||
|
|
||
| def address(person: Person) -> str: | ||
| return person.address | ||
|
|
||
|
|
||
| #EXERCISE 1: | ||
| #Add the is_adult code to the file you saved earlier. | ||
| #Run it through mypy - notice that no errors are reported - mypy understands that Person has a property named age so is happy with the function. | ||
| # Write a new function in the file that accepts a Person as a parameter and tries to access a property that doesn’t exist. Run it through mypy and check that it does report an error. | ||
|
|
||
| # SOLUTION: | ||
| # After run class-and-object.py, I get the following mypy errors: | ||
| # class-and-object.py:12: error: "Person" has no attribute "address" | ||
| # It indicates that the Person class does not have an attribute named address. | ||
|
|
||
| #EXERCISE 2: | ||
| # Add the is_adult code to the file you saved earlier | ||
| # Run it through mypy - notice that no errors are reported - mypy understands that Person has a property named age so is happy with the function. | ||
|
|
||
| # Write a new function in the file that accepts a Person as a parameter and tries to access a property that doesn’t exist. Run it through mypy and check that it does report an error. | ||
| # When running mypy, I get the following error: | ||
| # Class-and-object.py:27: error: "Person" has no attribute "address" [attr-defined] | ||
| # Found 1 error in 1 file (checked 1 source file) | ||
| # This error indicates that the Person class does not have an attribute named address. |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,25 @@ | ||
|
|
||
| # Write a Person class using @datatype which uses a datetime.date for date of birth, rather than an int for age. | ||
| # Re-add the is_adult method to it. | ||
|
|
||
| from datetime import date | ||
| from dataclasses import dataclass | ||
|
|
||
| @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 | ||
|
|
||
| # Adjust if birthday hasn't happened yet this year | ||
| if (today.month, today.day) < (self.date_of_birth.month, self.date_of_birth.day): | ||
| age -= 1 | ||
| return age >= 18 | ||
|
|
||
| jesus = Person("Jesus", date(1980, 1, 12), "Ubuntu") | ||
| print(jesus.is_adult()) | ||
|
|
|
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. You might want to change the validation to allow for reentry if the input was invalid, rather than exiting immediately |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,100 @@ | ||
|
|
||
| # EXERCISE 1: | ||
| # Write a program which: | ||
|
|
||
| # Already has a list of Laptops that a library has to lend out. | ||
| # Accepts user input to create a new Person - it should use the input function to read a person’s name, age, and preferred operating system. | ||
| # Tells the user how many laptops the library has that have that operating system. | ||
| # If there is an operating system that has more laptops available, tells the user that if they’re willing to accept that operating system they’re more likely to get a laptop. | ||
| # You should convert the age and preferred operating system input from the user into more constrained types as quickly as possible, and should output errors to stderr and terminate the program with a non-zero exit code if the user input bad values. | ||
|
|
||
| # SOLUTION: | ||
|
|
||
| import sys | ||
| from dataclasses import dataclass | ||
| from enum import Enum | ||
| from typing import List | ||
|
|
||
| class OperatingSystem(Enum): | ||
| MACOS = "macOS" | ||
| ARCH = "Arch Linux" | ||
| UBUNTU = "Ubuntu" | ||
|
|
||
| @dataclass(frozen=True) | ||
| class Person: | ||
| name: str | ||
| age: int | ||
| preferred_operating_system: OperatingSystem | ||
|
|
||
| @dataclass(frozen=True) | ||
| class Laptop: | ||
| id: int | ||
| manufacturer: str | ||
| model: str | ||
| screen_size_in_inches: float | ||
| operating_system: OperatingSystem | ||
|
|
||
| # Predefined laptops | ||
| laptops = [ | ||
| Laptop(id=1, manufacturer="Dell", model="XPS", screen_size_in_inches=13, operating_system=OperatingSystem.ARCH), | ||
| Laptop(id=2, manufacturer="Dell", model="XPS", screen_size_in_inches=15, operating_system=OperatingSystem.UBUNTU), | ||
| Laptop(id=3, manufacturer="Dell", model="XPS", screen_size_in_inches=15, operating_system=OperatingSystem.UBUNTU), | ||
| Laptop(id=4, manufacturer="Apple", model="macBook", screen_size_in_inches=13, operating_system=OperatingSystem.MACOS), | ||
| ] | ||
|
|
||
| def count_laptops_by_os(laptops: List[Laptop]) -> dict: | ||
| os_count = {os: 0 for os in OperatingSystem} | ||
| for laptop in laptops: | ||
| os_count[laptop.operating_system] += 1 | ||
| return os_count | ||
|
|
||
| def main(): | ||
| # --- Input name --- | ||
| name = input("Enter your name: ").strip() | ||
| if not name: | ||
| print("Error: Name cannot be empty.", file=sys.stderr) | ||
| sys.exit(1) | ||
|
|
||
| # --- Input age --- | ||
| age_input = input("Enter your age: ").strip() | ||
| try: | ||
| age = int(age_input) | ||
| if age <= 0: | ||
| raise ValueError() | ||
| except ValueError: | ||
| print("Error: Age must be a positive integer.", file=sys.stderr) | ||
| sys.exit(1) | ||
|
|
||
| # --- Input preferred OS --- | ||
| print("Choose preferred operating system:") | ||
| for os in OperatingSystem: | ||
| print(f"- {os.value}") | ||
| os_input = input("Enter OS: ").strip() | ||
|
|
||
| try: | ||
| preferred_os = OperatingSystem(os_input) | ||
| except ValueError: | ||
| print("Error: Invalid operating system.", file=sys.stderr) | ||
| sys.exit(1) | ||
|
|
||
| # --- Create Person --- | ||
| new_person = Person(name=name, age=age, preferred_operating_system=preferred_os) | ||
|
|
||
| # --- Count laptops by OS --- | ||
| os_count = count_laptops_by_os(laptops) | ||
|
|
||
| # --- Show count for user's preferred OS --- | ||
| count_for_user_os = os_count.get(new_person.preferred_operating_system, 0) | ||
| print(f"There are {count_for_user_os} laptops available with {new_person.preferred_operating_system.value}.") | ||
|
|
||
| # --- Find the OS with most laptops --- | ||
| max_os = max(os_count, key=os_count.get) | ||
| max_count = os_count[max_os] | ||
|
|
||
| # Suggest alternative if more laptops are available | ||
| if max_os != new_person.preferred_operating_system and max_count > count_for_user_os: | ||
| print(f"Tip: There are more laptops available with {max_os.value} ({max_count} laptops). " | ||
| f"If you're willing to accept that OS, you're more likely to get a laptop.") | ||
|
|
||
| if __name__ == "__main__": | ||
| main() |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,26 @@ | ||
|
|
||
| #EXERCISE 1: Fix the above code so that it works. You must not change the print on line 17 | ||
| # we do want to print the children’s ages. (Feel free to invent the ages of Imran’s children) | ||
|
|
||
| # SOLUTION: | ||
|
|
||
| from dataclasses import dataclass | ||
| from typing import List | ||
|
|
||
| @dataclass(frozen=True) | ||
| class Person: | ||
| name: str | ||
| age: int = 0 | ||
| children: List["Person"] | ||
|
|
||
| fatma = Person(name="Fatma", age=18, children=[]) | ||
| aisha = Person(name="Aisha", age=24, children=[]) | ||
|
|
||
| imran = Person(name="Imran", age=45, children=[fatma aisha]) | ||
|
|
||
| def print_family_tree(person: Person) -> None: | ||
| print(person.name) | ||
| for child in person.children: | ||
| print(f"- {child.name} ({child.age})") | ||
|
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 this print grandchildren recursively? |
||
|
|
||
| print_family_tree(imran) | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,51 @@ | ||
|
|
||
| #EXERCISE 1:: | ||
|
|
||
| #Play computer with this code. Predict what you expect each line will do. | ||
| #Then run the code and check your predictions. (If any lines cause errors, you may need to comment them out to check later lines). | ||
|
|
||
| #SOLUTION: | ||
|
|
||
| # The first four prints will work fine, as the Child class inherits from the Parent class and has access to its methods. | ||
| # The last four prints will get error because the Parent class does not have the methods get_full_name and change_last_name defined. | ||
| # Fix the code we can comment out the last four lines or we can add the methods to the Parent class. | ||
|
|
||
| class Parent: | ||
| def __init__(self, first_name: str, last_name: str): | ||
| self.first_name = first_name | ||
| self.last_name = last_name | ||
|
|
||
| def get_name(self) -> str: | ||
| return f"{self.first_name} {self.last_name}" | ||
|
|
||
|
|
||
| class Child(Parent): | ||
| def __init__(self, first_name: str, last_name: str): | ||
| super().__init__(first_name, last_name) | ||
| self.previous_last_names = [] | ||
|
|
||
| def change_last_name(self, last_name) -> None: | ||
| self.previous_last_names.append(self.last_name) | ||
| self.last_name = last_name | ||
|
|
||
| def get_full_name(self) -> str: | ||
| suffix = "" | ||
| if len(self.previous_last_names) > 0: | ||
| suffix = f" (née {self.previous_last_names[0]})" | ||
| return f"{self.first_name} {self.last_name}{suffix}" | ||
|
|
||
| person1 = Child("Elizaveta", "Alekseeva") | ||
| print(person1.get_name()) | ||
| print(person1.get_full_name()) | ||
| person1.change_last_name("Tyurina") | ||
| print(person1.get_name()) | ||
| print(person1.get_full_name()) | ||
|
|
||
| """ | ||
| person2 = Parent("Elizaveta", "Alekseeva") | ||
| print(person2.get_name()) | ||
| print(person2.get_full_name()) | ||
| person2.change_last_name("Tyurina") | ||
| print(person2.get_name()) | ||
| print(person2.get_full_name()) | ||
| """ |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,97 @@ | ||
| # his code contains bugs related to types. They are bugs mypy can catch. | ||
|
|
||
| # Read this code to understand what it’s trying to do. | ||
| # Add type annotations to the method parameters and return types of this code. | ||
| # Run the code through mypy, and fix all of the bugs that show up. | ||
| # When you’re confident all of the type annotations are correct, and the bugs are fixed, run the code and check it works. | ||
|
|
||
|
|
||
|
|
||
| def open_account(balances, name, amount): | ||
| 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, | ||
| } | ||
|
|
||
| # convert pounds to pence | ||
| open_account(balances, "Tobi", int(9.13 * 100)) # 913 pence | ||
| open_account(balances, "Olya", int(7.13 * 100)) # 713 pence | ||
|
|
||
| total_pence = sum_balances(balances) | ||
| total_string = format_pence_as_string(total_pence) | ||
|
|
||
| print(f"The bank accounts total {total_string}") | ||
|
|
||
|
|
||
|
|
||
|
|
||
| # When running mypy, I get the following errors: | ||
|
|
||
| # type-checking.py:24: error: Missing positional argument "amount" in call to "open_account" [call-arg] | ||
| # type-checking.py:25: error: Missing positional argument "amount" in call to "open_account" [call-arg] | ||
| # type-checking.py:28: error: Name "format_pence_as_str" is not defined [name-defined] | ||
| # type-checking.py:34: error: Missing positional argument "amount" in call to "open_account" [call-arg] | ||
| # type-checking.py:35: error: Missing positional argument "amount" in call to "open_account" [call-arg] | ||
| # type-checking.py:38: error: Name "format_pence_as_str" is not defined [name-defined] | ||
|
|
||
|
|
||
| # To fix this code, I need to add type annotations and correct the function calls as follows: | ||
|
|
||
| # wrong arguments to open_account, the function expects three arguments: balances, name, and amount. | ||
| # one call passes a string "£7.13" instead of a number. | ||
| # Wrong function name format_pence_as_str instead of format_pence_as_string. | ||
| # To keep the program consistent (Everything in pence), convert pounds to pence when opening accounts. | ||
|
|
||
| # Here is the corrected code: | ||
|
|
||
| """ | ||
| def open_account(balances, name, amount): | ||
| 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, | ||
| } | ||
|
|
||
| # convert pounds to pence | ||
| open_account(balances, "Tobi", int(9.13 * 100)) # 913 pence | ||
| open_account(balances, "Olya", int(7.13 * 100)) # 713 pence | ||
|
|
||
| total_pence = sum_balances(balances) | ||
| total_string = format_pence_as_string(total_pence) | ||
|
|
||
| print(f"The bank accounts total {total_string}") | ||
| """ | ||
|
|
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.
Very good answers!