generated from CodeYourFuture/Module-Template
-
-
Notifications
You must be signed in to change notification settings - Fork 42
London | 25-SDC-Nov | Aida Eslamimoghadam | Sprint 5 | Prep exercises #298
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
aydaeslami
wants to merge
17
commits into
CodeYourFuture:main
Choose a base branch
from
aydaeslami:PyType
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
17 commits
Select commit
Hold shift + click to select a range
c1e34e4
class and Objs
aydaeslami 0d47463
method
aydaeslami 1f1dd9d
DataClasses
aydaeslami 220bf3a
generic
aydaeslami bbf12ef
refactor
aydaeslami f81a6d0
Enum
aydaeslami 87cd1e2
MyType
aydaeslami f7aecec
Inheritance and Composition
aydaeslami a2237e7
Add recursive family tree traversal
aydaeslami edcb78d
Rename script to .py extension
aydaeslami 4c2d5d6
delete pre file
aydaeslami 96be26e
remove unused comment
aydaeslami 7e3d31b
Improve user input handling
aydaeslami f4d79b1
add type hint
aydaeslami 148842a
remove file cash and DS store
aydaeslami c343abe
Remove cached files and update gitignore
aydaeslami 7e427d7
Remove DS_Store from repository
aydaeslami 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,3 @@ | ||
| node_modules | ||
| __pycache__/ | ||
| *.pyc |
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,23 @@ | ||
| from dataclasses import dataclass | ||
| from typing import List | ||
|
|
||
| @dataclass(frozen=True) | ||
| class Person: | ||
| name: str | ||
| age:int | ||
| children: list['Person'] | ||
| Aida=Person(name="Aida",age=12, children=[]) | ||
| Anna=Person(name="Anna",age=12, children=[]) | ||
|
|
||
| fatma = Person(name="Fatma",age=12, children=[Anna]) | ||
| aisha = Person(name="Aisha",age=10, children=[Aida]) | ||
|
|
||
| imran = Person(name="Imran",age=40, children=[fatma, aisha]) | ||
|
|
||
|
|
||
| def print_family_tree(person: Person) -> None: | ||
| print(person.name) | ||
| for child in person.children: | ||
| print_family_tree(child) | ||
|
|
||
| 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,93 @@ | ||
| from typing import Iterable, Optional | ||
|
|
||
| class ImmutableNumberList: | ||
| def __init__(self, elements: Iterable[int]): | ||
| self.elements = [element for element in elements] | ||
|
|
||
| def first(self) -> Optional[int]: | ||
| if not self.elements: | ||
| return None | ||
| return self.elements[0] | ||
|
|
||
| def last(self) -> Optional[int]: | ||
| if not self.elements: | ||
| return None | ||
| return self.elements[-1] | ||
|
|
||
| def length(self) -> int: | ||
| return len(self.elements) | ||
|
|
||
| def largest(self) -> Optional[int]: | ||
| if not self.elements: | ||
| return None | ||
| largest = self.elements[0] | ||
| for element in self.elements: | ||
| if element > largest: | ||
| largest = element | ||
| return largest | ||
|
|
||
|
|
||
| class SortedImmutableNumberList(ImmutableNumberList): | ||
| def __init__(self, elements: Iterable[int]): | ||
| super().__init__(sorted(elements)) | ||
|
|
||
| def largest(self) -> Optional[int]: | ||
| return self.last() | ||
|
|
||
| def max_gap_between_values(self) -> Optional[int]: | ||
| if not self.elements: | ||
| return None | ||
| previous_element = None | ||
| max_gap = -1 | ||
| for element in self.elements: | ||
| if previous_element is not None: | ||
| gap = element - previous_element | ||
| if gap > max_gap: | ||
| max_gap = gap | ||
| previous_element = element | ||
| return max_gap | ||
|
|
||
|
|
||
| values = SortedImmutableNumberList([1, 20, 7, 13, 4]) | ||
| print(values.largest()) | ||
| print(values.max_gap_between_values()) | ||
|
|
||
| unsorted_values = ImmutableNumberList([1, 19, 7, 13, 4]) | ||
| print(unsorted_values.largest()) | ||
|
|
||
|
|
||
|
|
||
| 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_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,15 @@ | ||
| from datetime import date | ||
| thisYear = date.today().year | ||
|
|
||
|
|
||
| class Person: | ||
| def __init__(self, name: str, age: int, preferred_operating_system: str): | ||
| self.name = name | ||
| self.age = thisYear - age | ||
| self.preferred_operating_system = preferred_operating_system | ||
|
|
||
| def is_adult(self): | ||
| return self.age <= thisYear - 18 | ||
|
|
||
| imran = Person("Imran", 4, "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,28 @@ | ||
|
|
||
| from datetime import date | ||
| from dataclasses import dataclass | ||
|
|
||
| thisYear = date.today().year | ||
|
|
||
|
|
||
|
|
||
| @dataclass | ||
| class Person: | ||
| name: str | ||
| DoB: int | ||
| preferred_operating_system: str | ||
|
|
||
| def __init__(self, name: str, age: int, preferred_operating_system: str): | ||
| self.name = name | ||
| self.DoB = thisYear - age | ||
| self.preferred_operating_system = preferred_operating_system | ||
|
|
||
| def is_adult(self): | ||
| return self.DoB <= thisYear - 18 | ||
|
|
||
| imran = Person("Imran", 22, "Ubuntu") # We can call this constructor - @dataclass generated it for us. | ||
| print(imran) | ||
|
|
||
| imran2 = Person("Imran", 22, "Ubuntu") | ||
| print(imran2) | ||
| print(imran == imran2) # Prints True because they have the same DoB |
LonMcGregor marked this conversation as resolved.
Show resolved
Hide resolved
|
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,71 @@ | ||
| 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 | ||
|
|
||
|
|
||
|
|
||
|
|
||
| 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), | ||
| ] | ||
|
|
||
|
|
||
|
|
||
|
|
||
| namePerson = input("enter your personal name: ") | ||
| while True: | ||
| agePerson = input("Enter your age: ") | ||
| if agePerson.isdigit(): | ||
| agePerson = int(agePerson) | ||
| break | ||
| print("Invalid age, please try again.") | ||
|
|
||
|
|
||
| print("Available operating systems: MACOS, ARCH, UBUNTU") | ||
| while True: | ||
| preferredOS = input("Enter your preferred operating system: ").strip().upper() | ||
| try: | ||
| preferredOS = OperatingSystem[preferredOS] | ||
| break | ||
| except KeyError: | ||
| print("Invalid operating system, please try again.") | ||
|
|
||
| person=Person(name=namePerson, age=int(agePerson), preferred_operating_system=preferredOS) | ||
| possible_laptops = find_possible_laptops(laptops, person) | ||
| print(f"Possible laptops for {person.name}: {possible_laptops}") | ||
|
|
||
|
|
||
|
|
LonMcGregor marked this conversation as resolved.
Show resolved
Hide resolved
|
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,39 @@ | ||
| def triple(number : int)-> int: | ||
| return number * 3 | ||
|
|
||
| print(triple(10)) | ||
|
|
||
| # Bank account functions | ||
|
|
||
|
|
||
| def open_account( name:str, amount:float)->None: | ||
| balances[name] = int(amount * 100 ) | ||
|
|
||
|
|
||
|
|
||
| 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("Tobi", 9.13) | ||
| open_account("Olya", 7.13) | ||
|
|
||
| total_pence = sum_balances(balances) | ||
| total_string = format_pence_as_string(total_pence) | ||
|
|
||
| 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,31 @@ | ||
| class Person: | ||
| def __init__(self, name: str, age: int, address: str, preferred_operating_system: str): | ||
| self.name = name | ||
| self.age = age | ||
| self.address =address | ||
| self.preferred_operating_system = preferred_operating_system | ||
|
|
||
| def is_adult(person: Person) -> bool: | ||
| return person.age >= 18 | ||
|
|
||
| # def get_phone(person: Person) -> str: | ||
| # return person.phone_number | ||
|
|
||
| imran = Person("Imran", 22,"Shiraz", "Ubuntu") | ||
| print(imran.name) | ||
| print(imran.address) | ||
|
|
||
| aida = Person("Aida", 34, "Tehran", "Arch Linux") | ||
| print(aida.name) | ||
| print(aida.address) | ||
| print(is_adult(imran)) | ||
|
|
||
|
|
||
|
|
||
|
|
||
| # def get_phone(person: Person) -> str: | ||
| # return person.phone_number | ||
| # | ||
| # print(get_phone(aida)) | ||
| # Person.py:12: error: "Person" has no attribute "phone_number" [attr-defined] | ||
| # Found 1 error in 1 file (checked 1 source file) |
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 @@ | ||
| 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: | ||
|
|
||
| for os in person.preferred_operating_systems: | ||
| if laptop.operating_system.lower() == os.lower(): | ||
| possible_laptops.append(laptop) | ||
|
|
||
|
|
||
| return possible_laptops | ||
|
|
||
|
|
||
|
|
||
|
|
||
| people = [ | ||
| Person(name="Imran", age=22, preferred_operating_systems=["Ubuntu", "Arch Linux", "macOS", "Windows"]), | ||
| Person(name="Eliza", age=34, preferred_operating_systems=["Arch Linux", "macOS"]), | ||
| ] | ||
|
|
||
| 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("-----") | ||
| print(f"Laptops possible for {person.name}:") | ||
| for laptop in possible_laptops: | ||
| print(f"Possible laptop: {laptop.manufacturer} {laptop.id} {laptop.operating_system}") | ||
|
|
||
|
|
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.