generated from CodeYourFuture/Module-Template
-
-
Notifications
You must be signed in to change notification settings - Fork 42
Glasgow | 25-SDC-NOV | Katarzyna Kazimierczuk | Sprint 5 | prep #326
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
Open
katarzynakaz
wants to merge
6
commits into
CodeYourFuture:main
Choose a base branch
from
katarzynakaz:prep
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
22dccbc
prep
katarzynakaz 2e3be0b
prep fin
katarzynakaz 9bcad83
fixes from the reviewer implemented and files renamed
katarzynakaz 61f4c99
ignore .DS_Store files
katarzynakaz 597d675
remove .DS_Store files from repo
katarzynakaz 8b02f2f
trailing lines added, files renamed with _, DS_store removed
katarzynakaz File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1 +1,2 @@ | ||
| node_modules | ||
| .DS_Store |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1 @@ | ||
| .DS_Store |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,10 @@ | ||
| # ✍️exercise | ||
| # Predict what double("22") will do. Then run the code and check. Did it do what you expected? | ||
| # Why did it return the value it did? | ||
|
|
||
| def double(value): | ||
| return value * 2 | ||
|
|
||
| #2222 | ||
|
|
||
| print(double("22")) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,10 @@ | ||
| # ✍️exercise | ||
| # Read the above code and write down what the bug is. | ||
| # How would you fix it? | ||
|
|
||
| def double(number): | ||
| return number * 3 | ||
|
|
||
| print(double(10)) | ||
|
|
||
| #Either call it tripple or * 2 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,43 @@ | ||
| # ✍️exercise | ||
| # Do not run the following code. | ||
|
|
||
| # This 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}" | ||
| # File "C:\Users\kkazi\Desktop\cyf\Module-Tools\prep\3.py", line 27, in format_pence_as_string | ||
| # return f"£{pounds}.{pence:02d}" | ||
| #^^^^^^^^^^^ | ||
| #ValueError: Unknown format code 'd' for object of type 'float' | ||
| balances = { | ||
| "Sima": 700, | ||
| "Linn": 545, | ||
| "Georg": 831, | ||
| } | ||
|
|
||
| open_account(balances, "Tobi", 913) # to solve the float int issue value passed in in pence3.py:35: error: Missing positional argument "amount" in call to "open_account" [call-arg] | ||
| open_account(balances, "Olya", 713) #3.py:36: error: Missing positional argument "amount" in call to "open_account" [call-arg] | ||
|
|
||
| total_pence = sum_balances(balances) | ||
| total_string = format_pence_as_string(total_pence) # 3.py:39: error: Name "format_pence_as_str" is not defined [name-defined] | ||
| print(f"The bank accounts total {total_string}") |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,33 @@ | ||
| # Exercise | ||
| # 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. | ||
|
|
||
| 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)) | ||
| #error means prop does not exist | ||
|
|
||
| def get_eye_colour(person: Person): | ||
| return person.eye_colour | ||
|
|
||
| print(get_eye_colour(imran)) | ||
| #error in mypy no attribute |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,27 @@ | ||
| # exercise | ||
| # 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 | ||
|
|
||
| class Person: | ||
| def __init__(self, name: str, dob: date, preferred_operating_system: str): | ||
| self.name = name | ||
| self.dob = dob | ||
| self.preferred_operating_system = preferred_operating_system | ||
|
|
||
|
|
||
| def is_adult(self): | ||
| today = date.today() | ||
| age = date.today().year - self.dob.year | ||
| if (today.month, today.day) < (self.dob.month, self.dob.day): | ||
| age -= 1 | ||
| return age >= 18 | ||
|
|
||
| imran = Person("Imran", date(1988, 10, 10), "Ubuntu") | ||
| print(imran.is_adult()) | ||
| imran = Person("Jay", date(2015, 10, 10), "Ubuntu") | ||
| print(imran.is_adult()) | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,7 @@ | ||
| # exercise | ||
| # Think of the advantages of using methods instead of free functions. Write them down in your notebook. | ||
| 1 makes data and logic more organised | ||
| 2 makes it easier to find and reuse code | ||
| 3 makes the code more specific and it is easier to understand what it is doing for | ||
| a junior/learner like me :) | ||
| 4 reduces errors |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,30 @@ | ||
| # ✍️exercise | ||
| # 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 | ||
| class Person: | ||
| # def __init__(self, name: str, dob: date, preferred_operating_system: str): | ||
| # self.name = name | ||
| # self.dob = dob | ||
| # self.preferred_operating_system = preferred_operating_system | ||
| name: str | ||
| dob: date | ||
| preferred_operating_system: str | ||
|
|
||
| def is_adult(self): | ||
| today = date.today() | ||
| age = date.today().year - self.dob.year | ||
| if (today.month, today.day) < (self.dob.month, self.dob.day): | ||
| age -= 1 | ||
| return age >= 18 | ||
|
|
||
| imran = Person("Imran", date(1988, 10, 10), "Ubuntu") | ||
| print(imran.is_adult()) | ||
| imran = Person("Jay", date(2015, 10, 10), "Ubuntu") | ||
| print(imran.is_adult()) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,25 @@ | ||
| # ✍️exercise | ||
| # 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.) | ||
|
|
||
| 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=12, children=[]) | ||
| aisha = Person(name="Aisha", age=25, children=[]) | ||
|
|
||
| imran = Person(name="Imran", age=67, 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) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,61 @@ | ||
| # ✍️exercise | ||
| # Try changing the type annotation of Person.preferred_operating_system | ||
| # from str to List[str]. | ||
|
|
||
| # Run mypy on the code. | ||
|
|
||
| # It tells us different places that our code is now wrong, because we’re | ||
| # passing values of the wrong | ||
| # type. | ||
|
|
||
| # We probably also want to rename our field - lists are plural. Rename | ||
| # the field to preferred_operating_systems. | ||
|
|
||
| # Run mypy again. | ||
|
|
||
| # Fix all of the places that mypy tells you need changing. Make sure the | ||
| # program works as you’d expect. | ||
|
|
||
| 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}") | ||
|
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,115 @@ | ||
| # exercise | ||
| # 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. | ||
|
|
||
| from dataclasses import dataclass | ||
| from enum import Enum | ||
| from typing import List | ||
| from collections import Counter | ||
| import sys | ||
| from typing import Tuple | ||
|
|
||
| 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 create_a_new_person() -> Person: | ||
| add_name = input("What is your name? \n") | ||
| if not add_name or add_name == "" or len(add_name) > 50: | ||
| print("Enter a valid name.", file=sys.stderr) | ||
| sys.exit(1) | ||
|
|
||
| str_add_age = input("What is your age? \n") | ||
| if not str_add_age.isdigit(): | ||
| print("Enter a number.", file=sys.stderr) | ||
| sys.exit(1) | ||
| add_age = int(str_add_age) | ||
|
|
||
| if add_age <= 0 or add_age > 100: | ||
| print("Enter a valid age.", file=sys.stderr) | ||
| sys.exit(1) | ||
|
|
||
| add_os = input("What is your preferred operating system? \n") | ||
|
|
||
| if add_os not in [os.value for os in OperatingSystem]: | ||
| print("Enter a valid operating system.", file=sys.stderr) | ||
| sys.exit(1) | ||
|
|
||
| return Person(name=add_name, age=add_age, preferred_operating_system=OperatingSystem(add_os)) | ||
|
|
||
|
|
||
| def find_possible_laptops(laptops: List[Laptop], person: Person) -> Tuple[List[Laptop], List[Laptop]]: | ||
| possible_laptops = [] | ||
| other_laptops = [] | ||
| for laptop in laptops: | ||
| if laptop.operating_system == person.preferred_operating_system: | ||
| possible_laptops.append(laptop) | ||
| else: | ||
| other_laptops.append(laptop) | ||
| return possible_laptops, other_laptops | ||
katarzynakaz marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| def compare_counts_of_poss_and_others(possible_laptops: List[Laptop], other_laptops: List[Laptop], person: Person) -> None: | ||
| if len(other_laptops) == 0: | ||
| print("The os you chose has the biggest availability.") | ||
| return | ||
| # counter to get obj | ||
| other_laptops_counted_as_obj = Counter(laptop.operating_system for laptop in other_laptops) | ||
| # and here turn to tuple os+count | ||
| most_common_laptop = other_laptops_counted_as_obj.most_common(1)[0] | ||
katarzynakaz marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| if len(possible_laptops) < most_common_laptop[1]: | ||
| print(f"There are more laptops available with {most_common_laptop[0].value} than your preferred OS {person.preferred_operating_system.value}. Consider accepting that OS to increase your chances of getting a laptop.") | ||
| else: | ||
| print("Your chosen os has the biggest availability.") | ||
|
|
||
| people = [ | ||
| ] | ||
|
|
||
| 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), | ||
| ] | ||
|
|
||
| for person in people: | ||
| possible_laptops, other_laptops = find_possible_laptops(laptops, person) | ||
| print(f"Possible laptops for {person.name}: {possible_laptops}") | ||
|
|
||
|
|
||
| def execute(): | ||
| new_person = create_a_new_person() | ||
| possible_laptops, other_laptops = find_possible_laptops(laptops, new_person) | ||
| print(f"Number of laptops matching your preferred OS: {len(possible_laptops)}") | ||
| compare_counts_of_poss_and_others(possible_laptops, other_laptops, new_person) | ||
|
|
||
|
|
||
| execute() | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.