-
-
Notifications
You must be signed in to change notification settings - Fork 90
Glasgow | 26- SDC-Mar | Taras Mykytiuk | Sprint 5 | Prep exercises #514
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
5a9622a
150faad
e8b987d
550d3a4
1e56b0d
bb6b842
52e5b5a
7cd99f3
553c6a0
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,21 @@ | ||
| 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 | ||
|
|
||
|
|
||
| def is_adult(person: Person) -> bool: | ||
| return person.age >= 18 | ||
|
|
||
|
|
||
| imran = Person("Imran", 22, "Ubuntu") | ||
| print(imran.name) | ||
| # there is no property 'address' in Person class | ||
| # print(imran.address) | ||
| print(is_adult(imran)) | ||
|
|
||
| eliza = Person("Eliza", 34, "Arch Linux") | ||
| print(eliza.name) | ||
| # there is no property 'address' in Person class | ||
| # print(eliza.address) |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,24 @@ | ||
| from dataclasses import dataclass | ||
| from datetime import date | ||
|
|
||
|
|
||
| @dataclass(frozen=True) | ||
| class Person: | ||
| name: str | ||
| date_of_birth: date | ||
| preferred_operating_system: str | ||
|
|
||
| def is_adult(self) -> bool: | ||
| current_date = date.today() | ||
| full_years = current_date.year - self.date_of_birth.year | ||
| if (current_date.month < self.date_of_birth.month) or ( | ||
| current_date.month == self.date_of_birth.month | ||
| and current_date.day < self.date_of_birth.day | ||
| ): | ||
| full_years -= 1 | ||
| return full_years >= 18 | ||
|
|
||
|
|
||
| imran = Person("Imran", date(2008, 1, 30), "Ubuntu") | ||
| print(imran) | ||
| print("Imran is adult: " + str(imran.is_adult())) |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,17 @@ | ||
| def double(value): | ||
| return value * 2 | ||
|
|
||
|
|
||
| def double2(number): | ||
| return number * 3 | ||
|
|
||
|
|
||
| def main(): | ||
| # "22" is a string --> it will return string: "2222" | ||
| print(double("22")) | ||
| # in this function that expected to double the number, it multiplied by 3 ( probably accidental mistype) | ||
| print(double2(10)) # return 30 instead of 20 | ||
|
|
||
|
|
||
| if __name__ == "__main__": | ||
| main() |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,129 @@ | ||
| from enum import Enum | ||
|
|
||
| from dataclasses import dataclass | ||
| import sys | ||
| 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 | ||
|
|
||
|
|
||
| def operating_system_laptop_num( | ||
| laptops: List[Laptop], system: OperatingSystem | ||
| ) -> List[Laptop]: | ||
| sys_laptops = [] | ||
| for laptop in laptops: | ||
| if laptop.operating_system == system: | ||
| sys_laptops.append(laptop) | ||
| return sys_laptops | ||
|
|
||
|
|
||
| def inputPerson() -> dict: | ||
| name = input("Enter name: ") | ||
| age = int(input("Enter age: ")) | ||
| systemStr = input("Enter preferred operating system: ") | ||
| system = None | ||
| for system_variant in OperatingSystem: | ||
| if systemStr.lower() == system_variant.name.lower(): | ||
| system = system_variant | ||
| if system == None: | ||
| print("Incorrect operating system input.") | ||
| return {} | ||
|
|
||
| return {"name": name, "age": age, "system": system} | ||
|
|
||
|
|
||
| def main(): | ||
| input = inputPerson() | ||
| if len(input) == 0: | ||
|
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. Does your solution appropriately handle if there was an error in the input? |
||
| return | ||
|
|
||
| newPerson = Person(input["name"], input["age"], input["system"]) | ||
| 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, | ||
| ), | ||
| ] | ||
|
|
||
| preferred_laptops_num = len(find_possible_laptops(laptops, newPerson)) | ||
| max_other_laptops = 0 | ||
| other_laptops_sys = None | ||
| for system_variant in OperatingSystem: | ||
| if system_variant == input["system"]: | ||
| continue | ||
| laptops_num = len(operating_system_laptop_num(laptops, system_variant)) | ||
| if laptops_num > preferred_laptops_num: | ||
| max_other_laptops = laptops_num | ||
| other_laptops_sys = system_variant | ||
|
|
||
| print( | ||
| "We have " | ||
| + str(preferred_laptops_num) | ||
| + " with " | ||
| + input["system"].name | ||
| + " installed." | ||
| ) | ||
| if max_other_laptops > 0: | ||
| print( | ||
| "We also have " | ||
| + str(max_other_laptops) | ||
| + " with " | ||
| + other_laptops_sys.name | ||
| + " installed." | ||
| ) | ||
|
|
||
|
|
||
| if __name__ == "__main__": | ||
| main() | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,35 @@ | ||
| from dataclasses import dataclass | ||
| from typing import List | ||
| from datetime import date | ||
|
|
||
|
|
||
| @dataclass(frozen=True) | ||
| class Person: | ||
| name: str | ||
| date_of_birth: date | ||
| children: List["Person"] | ||
|
|
||
| def getAge(self) -> int: | ||
| current_date = date.today() | ||
| full_years = current_date.year - self.date_of_birth.year | ||
| if (current_date.month < self.date_of_birth.month) or ( | ||
| current_date.month == self.date_of_birth.month | ||
| and current_date.day < self.date_of_birth.day | ||
| ): | ||
| full_years -= 1 | ||
| return full_years | ||
|
|
||
|
|
||
| fatma = Person(name="Fatma", date_of_birth=date(2020, 4, 12), children=[]) | ||
| aisha = Person(name="Aisha", date_of_birth=date(2024, 7, 5), children=[]) | ||
|
|
||
| imran = Person(name="Imran", date_of_birth=date(2000, 1, 30), children=[fatma, aisha]) | ||
|
|
||
|
|
||
| def print_family_tree(person: Person) -> None: | ||
| print(person.name) | ||
| for child in person.children: | ||
| print(f"- {child.name} ({child.getAge()})") | ||
|
|
||
|
|
||
| print_family_tree(imran) |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,40 @@ | ||
| 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()) | ||
|
|
||
| # class person has no methods get_full_name() and change_last_name() | ||
| # when program will reach lines with this methods applied to person class it will throw an error | ||
| 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()) |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,26 @@ | ||
| from datetime import date | ||
|
|
||
|
|
||
|
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. The explanations of methods vs functions is missing |
||
| class Person: | ||
|
|
||
| def __init__(self, name: str, date_of_birth: date): | ||
| self.name = name | ||
| self.date_of_birth = date_of_birth | ||
|
|
||
| def is_adult(self) -> bool: | ||
| current_date = date.today() | ||
| full_years = current_date.year - self.date_of_birth.year | ||
| if (current_date.month < self.date_of_birth.month) or ( | ||
|
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. Is there a simpler way of comparing dates? |
||
| current_date.month == self.date_of_birth.month | ||
| and current_date.day < self.date_of_birth.day | ||
| ): | ||
| full_years -= 1 | ||
| return full_years >= 18 | ||
|
|
||
|
|
||
| imran = Person("Imran", date(2008, 1, 30)) | ||
| eliza = Person("Eliza", date(2088, 7, 30)) | ||
| someone = Person("Someone", date(2021, 7, 30)) | ||
| print("Imran is adult: " + str(imran.is_adult())) | ||
| print("Eliza is adult: " + str(eliza.is_adult())) | ||
| print("Someone is adult: " + str(someone.is_adult())) | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,33 @@ | ||
| def open_account(balances: dict, name: str, amount: int) -> None: | ||
| balances[name] = amount | ||
|
|
||
|
|
||
| def sum_balances(accounts: dict) -> 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_str(total_pence) | ||
|
|
||
| print(f"The bank accounts total {total_string}") |
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.
You don't need to commit these files, can you remove it / ignore it?