diff --git a/sprint5-prep-exercises/__pycache__/dataclasses.cpython-38.pyc b/sprint5-prep-exercises/__pycache__/dataclasses.cpython-38.pyc new file mode 100644 index 000000000..f151174f0 Binary files /dev/null and b/sprint5-prep-exercises/__pycache__/dataclasses.cpython-38.pyc differ diff --git a/sprint5-prep-exercises/classes_and_objects.py b/sprint5-prep-exercises/classes_and_objects.py new file mode 100644 index 000000000..235c5452c --- /dev/null +++ b/sprint5-prep-exercises/classes_and_objects.py @@ -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) diff --git a/sprint5-prep-exercises/dataclasses_ex.py b/sprint5-prep-exercises/dataclasses_ex.py new file mode 100644 index 000000000..fa46975d4 --- /dev/null +++ b/sprint5-prep-exercises/dataclasses_ex.py @@ -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())) diff --git a/sprint5-prep-exercises/double.py b/sprint5-prep-exercises/double.py new file mode 100644 index 000000000..88376dc93 --- /dev/null +++ b/sprint5-prep-exercises/double.py @@ -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() diff --git a/sprint5-prep-exercises/enums.py b/sprint5-prep-exercises/enums.py new file mode 100644 index 000000000..b057f527a --- /dev/null +++ b/sprint5-prep-exercises/enums.py @@ -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: + 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() diff --git a/sprint5-prep-exercises/generics.py b/sprint5-prep-exercises/generics.py new file mode 100644 index 000000000..cb8b14230 --- /dev/null +++ b/sprint5-prep-exercises/generics.py @@ -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) diff --git a/sprint5-prep-exercises/inheritance.py b/sprint5-prep-exercises/inheritance.py new file mode 100644 index 000000000..cd8ebb465 --- /dev/null +++ b/sprint5-prep-exercises/inheritance.py @@ -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()) diff --git a/sprint5-prep-exercises/methods.py b/sprint5-prep-exercises/methods.py new file mode 100644 index 000000000..3d1cf76df --- /dev/null +++ b/sprint5-prep-exercises/methods.py @@ -0,0 +1,26 @@ +from datetime import date + + +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 ( + 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())) diff --git a/sprint5-prep-exercises/type_checking.py b/sprint5-prep-exercises/type_checking.py new file mode 100644 index 000000000..7496d3035 --- /dev/null +++ b/sprint5-prep-exercises/type_checking.py @@ -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}") diff --git a/sprint5-prep-exercises/type_guided_refactorings.py b/sprint5-prep-exercises/type_guided_refactorings.py new file mode 100644 index 000000000..7645f71c8 --- /dev/null +++ b/sprint5-prep-exercises/type_guided_refactorings.py @@ -0,0 +1,67 @@ +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: + if laptop.operating_system in 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_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(f"Possible laptops for {person.name}: {possible_laptops}")