generated from CodeYourFuture/Module-Template
-
-
Notifications
You must be signed in to change notification settings - Fork 22
London | Rihanna Poursoltani | Module-Decomposition | Sprint 4 | Prep exercises #56
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
RihannaP
wants to merge
11
commits into
CodeYourFuture:main
Choose a base branch
from
RihannaP:Prep-exercises
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
11 commits
Select commit
Hold shift + click to select a range
94faa6d
Intial setup
RihannaP 6ce33d1
Add exercise 1
RihannaP 9b04107
Implement intial code
RihannaP 24cdc64
Implemment Ex2
RihannaP 630fb33
Implement Ex3
RihannaP 7940cc9
Implement Ex4
RihannaP c30cdaa
Implement Ex5
RihannaP 0d29c6b
Implement Ex6
RihannaP 91b5ffd
Implement Ex7
RihannaP cb4c34c
Implement Ex8
RihannaP d1131eb
Implement Ex9
RihannaP 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,19 @@ | ||
| def double(number): | ||
| return number * 3 | ||
|
|
||
| print(double(10)) | ||
|
|
||
| # Here the name of the function is double but on line 3 it returns the triple of the a given number | ||
| # Fix version | ||
|
|
||
| def triple(number): | ||
| return number * 3 | ||
|
|
||
| print(triple(10)) | ||
|
|
||
| # or | ||
|
|
||
| def double(number): | ||
| return number * 2 | ||
|
|
||
| print(double(10)) |
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,41 @@ | ||
| from typing import List, Dict, Union | ||
|
|
||
| def open_account(balances: dict[str, int], name: str, amount: Union[int, float, str]) -> None : | ||
| if isinstance(amount, str) and amount.startswith("£"): | ||
| pounds, pence = map(int, amount[1:].split(".")) | ||
| amount = pounds * 100 + pence | ||
| elif isinstance(amount, float): | ||
| amount = int(round(amount * 100)) | ||
| elif isinstance(amount, int): | ||
| pass | ||
| else: | ||
| raise ValueError("Unsupported amount format") | ||
| 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_str(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", 9.13) | ||
| open_account(balances,"Olya", "£7.13") | ||
|
|
||
| total_pence:int = sum_balances(balances) | ||
| total_string: str = format_pence_as_str(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,27 @@ | ||
| 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) | ||
|
|
||
| # error: "Person" has no attribute "address" | ||
| # error: "Person" has no attribute "address" | ||
|
|
||
| def is_adult(person: Person) -> bool: | ||
| return person.age >= 18 | ||
|
|
||
| print(is_adult(imran)) | ||
| # no error | ||
|
|
||
|
|
||
| def get_city(person: Person) -> str: | ||
| return person.location | ||
| #person has no attribute location. | ||
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,18 @@ | ||
| 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(person: Person) -> bool: | ||
| today = date.today() | ||
| dob = person.date_of_birth | ||
| age = today.year - dob.year - ((today.month, today.day) < (dob.month, dob.day)) | ||
| return age >= 18 | ||
|
|
||
| imran_dob = date(2003, 9, 10) | ||
| imran = Person("Imran", imran_dob, "Ubuntu") | ||
|
|
||
| print(is_adult(imran)) # True |
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 @@ | ||
| 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: | ||
RihannaP marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| today = date.today() | ||
| dob = self.date_of_birth | ||
| age = today.year - dob.year - ((today.month, today.day) < (dob.month, dob.day)) | ||
| return age >= 18 | ||
|
|
||
|
|
||
| imran = Person(name="Imran", date_of_birth=date(2003, 9, 10), preferred_operating_system="Ubuntu") | ||
|
|
||
| print(imran.is_adult()) # Output: True | ||
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 @@ | ||
| from dataclasses import dataclass | ||
| from datetime import date | ||
| from typing import List | ||
|
|
||
| @dataclass(frozen=True) | ||
| class Person: | ||
| name: str | ||
| date_of_birth: date | ||
| children: List["Person"] | ||
|
|
||
| @property | ||
| def age(self) -> int: | ||
| today = date.today() | ||
| dob = self.date_of_birth | ||
| return today.year - dob.year - ((today.month, today.day) < (dob.month, dob.day)) | ||
|
|
||
| def print_family_tree(person: Person, indent: int = 0) -> None: | ||
| spacer = " " * indent | ||
| print(f"{spacer}{person.name} (Age: {person.age})") | ||
| for child in person.children: | ||
| print_family_tree(child, indent + 1) | ||
|
|
||
| fatma = Person(name="Fatma", date_of_birth=date(2012, 5, 14), children=[]) | ||
| aisha = Person(name="Aisha", date_of_birth=date(2015, 8, 3), children=[]) | ||
| imran = Person(name="Imran", date_of_birth=date(1985, 2, 20), children=[fatma, aisha]) | ||
|
|
||
| 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,41 @@ | ||
| 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_systems: List[str] | ||
|
|
||
|
|
||
| def find_possible_laptops(laptops: List[Laptop], person: Person) -> List[Laptop]: | ||
| possible_laptops = [] | ||
| for laptop in laptops: | ||
| if laptop.operating_systems == 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_systems=["Arch Linux"]), | ||
| Laptop(id=2, manufacturer="Dell", model="XPS", screen_size_in_inches=15, operating_systems=["Ubuntu"]), | ||
| Laptop(id=3, manufacturer="Dell", model="XPS", screen_size_in_inches=15, operating_systems=["ubuntu"]), | ||
| Laptop(id=4, manufacturer="Apple", model="macBook", screen_size_in_inches=13, operating_systems=["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,88 @@ | ||
| import sys | ||
| from dataclasses import dataclass | ||
| from enum import Enum | ||
| from typing import List | ||
| from collections import Counter | ||
|
|
||
|
|
||
| class OperatingSystem(Enum): | ||
| MACOS = "macOS" | ||
| ARCH = "Arch Linux" | ||
| UBUNTU = "Ubuntu" | ||
|
|
||
| @classmethod | ||
| def from_string(cls, s: str): | ||
| for os in cls: | ||
| if os.value.lower() == s.lower(): | ||
| return os | ||
| raise ValueError(f"Unsupported operating system: {s}") | ||
|
|
||
|
|
||
| @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]: | ||
| return [laptop for laptop in laptops if laptop.operating_system == person.preferred_operating_system] | ||
|
|
||
|
|
||
| def main(): | ||
| # Existing inventory | ||
| 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), | ||
| ] | ||
|
|
||
| # Read and validate user input | ||
| try: | ||
| name = input("Enter your name: ").strip() | ||
| if not name: | ||
| raise ValueError("Name cannot be empty.") | ||
|
|
||
| age_input = input("Enter your age: ").strip() | ||
| age = int(age_input) | ||
| if age <= 0: | ||
| raise ValueError("Age must be a positive integer.") | ||
|
|
||
| print("Available operating systems:") | ||
| for os in OperatingSystem: | ||
| print(f"- {os.value}") | ||
| os_input = input("Enter your preferred operating system: ").strip() | ||
| preferred_os = OperatingSystem.from_string(os_input) | ||
|
|
||
| except ValueError as e: | ||
| print(f"Error: {e}", file=sys.stderr) | ||
| sys.exit(1) | ||
|
|
||
| # Create the person | ||
| person = Person(name=name, age=age, preferred_operating_system=preferred_os) | ||
|
|
||
| # Check availability | ||
| possible_laptops = find_possible_laptops(laptops, person) | ||
| print(f"\nThere are {len(possible_laptops)} laptops available with {preferred_os.value}.") | ||
|
|
||
| # Count OS distribution | ||
| os_counts = Counter(laptop.operating_system for laptop in laptops) | ||
| most_common_os, most_common_count = os_counts.most_common(1)[0] | ||
|
|
||
| if most_common_os != preferred_os and most_common_count > len(possible_laptops): | ||
| print(f"Note: There are more laptops available with {most_common_os.value} ({most_common_count} total).") | ||
| print(f"If you're willing to accept {most_common_os.value}, you're more likely to get a laptop.") | ||
|
|
||
|
|
||
| if __name__ == "__main__": | ||
| main() |
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,49 @@ | ||
| 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()) | ||
|
|
||
|
|
||
| #W/O commenting output: | ||
| #Elizaveta Alekseeva | ||
| #Elizaveta Alekseeva | ||
| #Elizaveta Tyurina | ||
| #Elizaveta Alekseeva | ||
| #Elizaveta Tyurina (née Alekseeva) | ||
| #Traceback (most recent call last): | ||
| # File "/Users/Module-Decomposition/prep_exercises/exercise9inheritance.py", line 34, in <module> | ||
| # print(person2.get_full_name()) | ||
| #AttributeError: 'Parent' object has no attribute 'get_full_name' | ||
RihannaP marked this conversation as resolved.
Show resolved
Hide resolved
|
||
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.