-
-
Notifications
You must be signed in to change notification settings - Fork 109
London| 26-Jul-SDC | Shaghayegh far| Sprint 5 | Prep Exercises #664
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
shaghayeghfar
wants to merge
1
commit into
CodeYourFuture:main
Choose a base branch
from
shaghayeghfar:Sprint5
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
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 |
|---|---|---|
| @@ -0,0 +1,95 @@ | ||
|
|
||
| #enum exersice | ||
|
|
||
| from dataclasses import dataclass | ||
| from typing import List | ||
| import sys | ||
|
|
||
|
|
||
| @dataclass(frozen=True) | ||
| class Person: | ||
| name: str | ||
| age: int | ||
| preferred_operating_system: str | ||
|
|
||
|
|
||
| @dataclass(frozen=True) | ||
| class Laptop: | ||
| id: int | ||
| manufacturer: str | ||
| model: str | ||
| screen_size_in_inches: float | ||
| operating_system: str | ||
|
|
||
|
|
||
| laptops = [ | ||
| Laptop(1, "Dell", "XPS", 13, "Ubuntu"), | ||
| Laptop(2, "Dell", "XPS", 15, "Ubuntu"), | ||
| Laptop(3, "Dell", "XPS", 15, "Arch Linux"), | ||
| Laptop(4, "Apple", "MacBook", 13, "macOS"), | ||
| Laptop(5, "Lenovo", "ThinkPad", 14, "Ubuntu"), | ||
| ] | ||
|
|
||
|
|
||
| name = input("Enter your name: ") | ||
|
|
||
| try: | ||
| age = int(input("Enter your age: ")) | ||
| except ValueError: | ||
| print("Error: age must be a number.", file=sys.stderr) | ||
| sys.exit(1) | ||
|
|
||
|
|
||
| preferred_operating_system = input( | ||
| "Enter your preferred operating system: " | ||
| ) | ||
|
|
||
| available_operating_systems = { | ||
| laptop.operating_system for laptop in laptops | ||
| } | ||
|
|
||
| if preferred_operating_system not in available_operating_systems: | ||
| print( | ||
| "Error: that operating system is not available.", | ||
| file=sys.stderr | ||
| ) | ||
| sys.exit(1) | ||
|
|
||
|
|
||
| person = Person( | ||
| name=name, | ||
| age=age, | ||
| preferred_operating_system=preferred_operating_system | ||
| ) | ||
|
|
||
|
|
||
| matching_laptops = [ | ||
| laptop | ||
| for laptop in laptops | ||
| if laptop.operating_system == person.preferred_operating_system | ||
| ] | ||
|
|
||
| print( | ||
| f"The library has {len(matching_laptops)} " | ||
| f"laptop(s) with {person.preferred_operating_system}." | ||
| ) | ||
|
|
||
|
|
||
| laptop_counts = {} | ||
|
|
||
| for laptop in laptops: | ||
| laptop_counts[laptop.operating_system] = ( | ||
| laptop_counts.get(laptop.operating_system, 0) + 1 | ||
| ) | ||
|
|
||
|
|
||
| for operating_system, count in laptop_counts.items(): | ||
| if ( | ||
| operating_system != person.preferred_operating_system | ||
| and count > len(matching_laptops) | ||
| ): | ||
| print( | ||
| f"The library has more {operating_system} laptops " | ||
| f"({count}). You are more likely to get a laptop " | ||
| f"if you are willing to use {operating_system}." | ||
| ) | ||
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 @@ | ||
|
|
||
| # 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? | ||
|
|
||
|
|
||
| # double("22") returns "2222". | ||
|
|
||
| # "22" is a string, so * 2 repeats the string twice. | ||
|
|
||
|
|
||
| def half(value): | ||
| return value / 2 | ||
|
|
||
| def double(value): | ||
| return value * 2 | ||
|
|
||
| def second(value): | ||
| return value[1] | ||
|
|
||
| print(double(22)) | ||
| print(double("hello")) | ||
| print(double("22")) | ||
|
|
||
| print(second(22)) | ||
| print(second(0x16)) | ||
| print(second("hello")) | ||
| print(second("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,51 @@ | ||
| # Inheritance exercise. | ||
|
|
||
| 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()) | ||
|
|
||
| # Parent does not have get_full_name() | ||
| # print(person2.get_full_name()) | ||
|
|
||
| # Parent does not have change_last_name() | ||
| # person2.change_last_name("Tyurina") | ||
|
|
||
| # These would work if the above line was not an error: | ||
| # print(person2.get_name()) | ||
| # print(person2.get_full_name()) |
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,13 @@ | ||
|
|
||
| # Read the above code and write down what the bug is. How would you fix it? | ||
|
|
||
| # The bug was that the function multiplied the number by 3. | ||
|
|
||
| # Since the function is called double, it should multiply the number by 2. | ||
|
|
||
| def double(number): | ||
| return number * 2 | ||
|
|
||
| print(double(10)) | ||
|
|
||
|
|
|
Contributor
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 check this file with mypy? I get an error when I try to check it |
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,53 @@ | ||
| # type checking | ||
|
|
||
| rom 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}") | ||
|
|
||
| # Answer: | ||
|
|
||
| # The type annotations tell mypy what types are expected. | ||
|
|
||
| # balances contains string names and integer balances. | ||
|
|
||
| # The account amounts are stored as pence, so £9.13 is 913 | ||
|
|
||
| # and £7.13 is 713. | ||
|
|
||
| # format_pence_as_string returns a string. | ||
|
|
||
| # The original function call had the wrong function name: | ||
|
|
||
| # format_pence_as_str -> format_pence_as_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,36 @@ | ||
| # Classes and objects | ||
|
|
||
| # Classes and objects | ||
|
|
||
| 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)) | ||
|
|
||
|
|
||
|
|
||
|
|
||
| def is_developer(person: Person) -> bool: | ||
| return person.is_developer | ||
|
|
||
|
|
||
| print(is_developer(imran)) | ||
|
|
||
| # there is an error because the is_developer attribute is not in the Person class. |
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,12 @@ | ||
| # methods | ||
|
|
||
| # Think of the advantages of using methods | ||
|
|
||
| Encapsulation: | ||
| Data and methods are kept together in one class, which controls how the data can be accessed or changed. It hides the implementation details and allows the implementation to change without affecting the user, as long as the interface stays the same. | ||
| For example, a Person class can control how a person's data is modified. | ||
|
|
||
|
|
||
| Ease of use: | ||
| Users only need to know how to use the class's interface, not how it works internally. Methods can be easily accessed using dot notation and IDE autocomplete, | ||
| for example person.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,19 @@ | ||
|
|
||
| # 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. | ||
|
|
||
| import datetime as dt | ||
|
|
||
| class Person: | ||
| def __init__(self, name: str, birthdate: dt.date, preferred_operating_system: str): | ||
| self.name = name | ||
| self.birthdate = birthdate | ||
| self.preferred_operating_system = preferred_operating_system | ||
| self.birthdate = birthdate | ||
|
|
||
| def is_adult(self) -> bool: | ||
| today = dt.date.today() | ||
| print(today) | ||
| return today >= dt.date(self.birthdate.year +18, self.birthdate.month, self.birthdate.day) | ||
|
|
||
| imran = Person("Imran", dt.date(2008,8,6), "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,35 @@ | ||
|
|
||
| # Write a Person class using @datatype which uses a datetime.date for date of birth, rather than an int for age. | ||
|
|
||
| from dataclasses import dataclass | ||
| from datetime import date | ||
|
|
||
|
|
||
| @dataclass | ||
| class Person: | ||
| name: str | ||
| date_of_birth: date | ||
| preferred_operating_system: str | ||
|
|
||
| def is_adult(self) -> bool: | ||
| today = date.today() | ||
|
|
||
| years = today.year - self.date_of_birth.year | ||
|
|
||
| had_birthday_this_year = ( | ||
| (today.month, today.day) | ||
| >= (self.date_of_birth.month, self.date_of_birth.day) | ||
| ) | ||
|
|
||
| age = years if had_birthday_this_year else years - 1 | ||
|
|
||
| return age >= 18 | ||
|
|
||
|
|
||
| imran = Person( | ||
| "Imran", | ||
| date(2008, 8, 6), | ||
| "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,26 @@ | ||
| # Generic exersice | ||
|
|
||
| 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=5, children=[]) | ||
| aisha = Person(name="Aisha", age=8, children=[]) | ||
|
|
||
| imran = Person(name="Imran", age=35, 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) |
Oops, something went wrong.
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.
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.
Is there a way you could improve the UX of submitting some of these inputs, like OS?