-
-
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
base: main
Are you sure you want to change the base?
Changes from all commits
c1e34e4
0d47463
1f1dd9d
220bf3a
bbf12ef
f81a6d0
87cd1e2
f7aecec
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
|
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 should not commit cache files like this, can you figure out how to remove them? |
| 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=12, children=[]) | ||
| aisha = Person(name="Aisha",age=10, children=[]) | ||
|
|
||
| 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(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) | ||
|
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. is this meant to be a python file? is the name correct? |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,97 @@ | ||
| 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()) | ||
| # print(unsorted_values.max_gap_between_values()). 'ImmutableNumberList' object has no attribute 'max_gap_between_values' | ||
|
|
||
|
|
||
|
|
||
| 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()) 'Parent' object has no attribute 'get_full_name' | ||
| # person2.change_last_name("Tyurina") 'Parent' object has no attribute 'change_last_name' | ||
| print(person2.get_name()) | ||
| # print(person2.get_full_name()) 'Parent' object has no attribute 'get_full_name' |
|
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,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()) |
| 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 |
|
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 add some data validation to the input allowing for reentry. You might want to offer a prompt indicating the choices available for operating system. If I type simply 'Ubuntu' as an OS it does not work, can you find out why? |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,64 @@ | ||
| 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: ") | ||
| try: | ||
| agePerson = input("enter your age: ") | ||
| except ValueError: | ||
| print("Invalid age. Age must be a number.") | ||
| exit() | ||
| try: | ||
| preferredOS = input("enter your preferred operating system: ") | ||
| except KeyError: | ||
| print("Invalid operating system entered. Please choose from: MACOS, ARCH, UBUNTU.") | ||
| exit() | ||
|
|
||
| person=Person(name=namePerson, age=int(agePerson), preferred_operating_system=OperatingSystem[preferredOS]) | ||
| possible_laptops = find_possible_laptops(laptops, person) | ||
| print(f"Possible laptops for {person.name}: {possible_laptops}") | ||
|
|
||
|
|
||
|
|
|
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. Have you considered also adding return types? |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,40 @@ | ||
| def triple(number): | ||
| return number * 3 | ||
|
|
||
| print(triple(10)) | ||
|
|
||
| # Bank account functions | ||
|
|
||
|
|
||
| def open_account( name:str, amount:float): | ||
| balances["name"] = int(amount * 100 ) | ||
|
|
||
|
|
||
|
|
||
|
|
||
| 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, | ||
| } | ||
| 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}") |
| 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) |
| 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}") | ||
|
|
||
|
|
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.
DS Store files don't need to be commited either