-
-
Notifications
You must be signed in to change notification settings - Fork 42
London | 25-SDC-Nov | Emiliano Uruena | Sprint 5 | Prep Exercises #306
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 |
|---|---|---|
| @@ -1 +1,5 @@ | ||
| node_modules | ||
| implement-cowsay/.venv | ||
| **/package.json | ||
| **/package-lock.json | ||
| implement-cowsay/requirements.txt |
This file was deleted.
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,40 @@ | ||
| ''' | ||
| 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. | ||
| ''' | ||
|
|
||
| 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) | ||
| #print(imran.address) | ||
|
|
||
| eliza = Person("Eliza", 34, "Arch Linux") | ||
| print(eliza.name) | ||
| #print(eliza.address) | ||
|
|
||
| def is_adult(person: Person) -> bool: | ||
| return person.age >= 18 | ||
|
|
||
| print(is_adult(imran)) | ||
|
|
||
| ''' adding the function:''' | ||
| def read_genre(person: Person) -> str: | ||
| if person.genre == "M": | ||
| return "Male" | ||
| else: | ||
| return "Female" | ||
|
|
||
| ''' returns: | ||
| cyf@cyfs-MacBook-Pro prep-sprint5 % mypy class-object-person.py | ||
| class-object-person.py:29: error: "Person" has no attribute "genre" [attr-defined] | ||
| Found 1 error in 1 file (checked 1 source file) | ||
|
|
||
| ''' |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,27 @@ | ||
| '''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 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_year = date.today().year | ||
| birth_year = self.date_of_birth.year | ||
| age = today_year - birth_year | ||
|
|
||
| if date.today().month > self.date_of_birth.month: | ||
| return age >= 18 | ||
| elif date.today().month == self.date_of_birth.month: | ||
| return date.today().day >= self.date_of_birth.day and age >= 18 | ||
| else: | ||
| return age - 1 >= 18 | ||
|
|
||
| imran = Person("Imran", date(2002, 9, 21), "Ubuntu") | ||
| print(imran) | ||
| print("Is adult?", imran.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. The output here could be neater, it is generally not a good idea to print objects directly to user facing interfaces. Good idea to allow using numbers for quicker data entry. You might want to consider adding a bit more data validation in case the input is incorrect, e.g. if I type a letter instead of a number for age |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,108 @@ | ||
| ''' | ||
| Write a program which: | ||
| 1.Already has a list of Laptops that a library has to lend out. | ||
| 2.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. | ||
| 3.Tells the user how many laptops the library has that have that operating system. | ||
| 4.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. | ||
| ''' | ||
|
|
||
| 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 | ||
|
|
||
|
|
||
| def find_possible_laptops(laptops: List[Laptop], person: Person) -> List[Laptop]: | ||
| possible_laptops = [] | ||
| for laptop in laptops: | ||
| if laptop.operating_system == person.preferred_operating_system: | ||
| possible_laptops.append(laptop) | ||
| return possible_laptops | ||
|
|
||
|
|
||
| people = [ | ||
| Person(name="Imran", age=22, preferred_operating_system=OperatingSystem.UBUNTU), | ||
| Person(name="Eliza", age=34, preferred_operating_system=OperatingSystem.ARCH), | ||
| ] | ||
|
|
||
| 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), | ||
| ] | ||
|
|
||
| print("Library laptops:") # 1. list of laptops | ||
| for laptop in laptops: | ||
| print(f"- ID:{laptop.id} {laptop.operating_system.value} - {laptop.manufacturer} {laptop.model} Size: {laptop.screen_size_in_inches}") | ||
|
|
||
| # receiving values from input to create person: | ||
| name = input("Enter your name: ") | ||
| age = int(input("Enter your age: ")) | ||
|
|
||
| print("Choose preferred operating system:") | ||
| print("1. macOS") | ||
| print("2. Arch Linux") | ||
| print("3. Ubuntu") | ||
| choice = input(">> ") | ||
|
|
||
| os_map = { | ||
| "1": OperatingSystem.MACOS, | ||
| "2": OperatingSystem.ARCH, | ||
| "3": OperatingSystem.UBUNTU, | ||
| } | ||
|
|
||
| preferred_os = os_map.get(choice) | ||
|
|
||
| person = Person(name=name, age=age, preferred_operating_system=preferred_os) | ||
|
|
||
| print("Created person:", person) | ||
| # counts how many laptops there are with that OS | ||
| matches_count = sum( | ||
| 1 for laptop in laptops | ||
| if laptop.operating_system == person.preferred_operating_system | ||
| ) | ||
| print(f"There are {matches_count} laptop(s) with {person.preferred_operating_system.value}.") | ||
|
|
||
| count_laptops_by_os = { | ||
| os_type: sum(1 for laptop in laptops if laptop.operating_system == os_type) | ||
| for os_type in OperatingSystem | ||
| } | ||
|
|
||
| best_os = max(count_laptops_by_os, key = count_laptops_by_os.get) | ||
| best_os_count = count_laptops_by_os[best_os] | ||
|
|
||
| if best_os != person.preferred_operating_system: | ||
| print( | ||
| f"There are more laptops available with {best_os.value} " | ||
| f"({best_os_count} laptops). If you're willing to use that OS, " | ||
| "you're more likely to get a laptop." | ||
| ) | ||
| else: | ||
| print("Good choice! Your preferred OS has the highest availability.") | ||
|
|
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,21 @@ | ||
| from dataclasses import dataclass | ||
| from typing import List | ||
|
|
||
| @dataclass(frozen=True) | ||
| class Person: | ||
| name: str | ||
| age: int | ||
| children: List["Person"] | ||
|
|
||
| fatma = Person(name="Fatma", age=20, children=[]) | ||
| aisha = Person(name="Aisha", age=30, children=[]) | ||
|
|
||
| imran = Person(name="Imran", age=50, children=[fatma, aisha]) | ||
| maria = Person(name="maria", age=38,children=[fatma]) | ||
|
|
||
| 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(maria) | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,47 @@ | ||
| ''' | ||
| 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). | ||
| ''' | ||
|
|
||
| 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()) | ||
|
|
||
| # PREDICTION: | ||
| # Printing from person1 (Child) will work fine, as the Child class inherits from the Parent class and has access to its own methods. | ||
| # Printing from person2 (Parent) will get error because the Parent class does not have the methods get_full_name and change_last_name defined in the Child class. |
|
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 describe the difference and any dis/advantage of using a class method vs a function? |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,24 @@ | ||
|
|
||
| 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 | ||
|
|
||
| def is_adult(self) -> bool: | ||
| today_year = date.today().year | ||
| birth_year = self.date_of_birth.year | ||
| age = today_year - birth_year | ||
|
|
||
| if date.today().month > self.date_of_birth.month: | ||
| return age >= 18 | ||
| elif date.today().month == self.date_of_birth.month: | ||
| return date.today().day >= self.date_of_birth.day and age >= 18 | ||
| else: | ||
| return age - 1 >= 18 | ||
|
|
||
|
|
||
| imran = Person("Imran", date(2007,1,19), "Ubuntu") | ||
| print(imran.is_adult()) |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,30 @@ | ||
| def open_account(balances:dict, name:str, amount:int) -> None: | ||
| balances[name] = amount | ||
|
|
||
| def sum_balances(accounts) -> 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", int(9.13 * 100)) | ||
| open_account(balances,"Olya", int(7.13 * 100)) | ||
|
|
||
| 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.
You should not need to change files in other directories, is there a reason you changed this?