diff --git a/.gitignore b/.gitignore index 3c3629e6..59cc0d26 100644 --- a/.gitignore +++ b/.gitignore @@ -1 +1,3 @@ node_modules +/.venv +implement-cowsay/.venv/ \ No newline at end of file diff --git a/.vscode/settings.json b/.vscode/settings.json new file mode 100644 index 00000000..3a650214 --- /dev/null +++ b/.vscode/settings.json @@ -0,0 +1,4 @@ +{ + "python.analysis.typeCheckingMode": "basic", + "python.analysis.autoImportCompletions": true +} diff --git a/sprint5-prep/prep2-exercise.py b/sprint5-prep/prep2-exercise.py new file mode 100644 index 00000000..885099af --- /dev/null +++ b/sprint5-prep/prep2-exercise.py @@ -0,0 +1,33 @@ +def open_account(balances: dict[str, int], name: str, amount: int) -> None: + 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_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(balances, "Tobi", 913) +open_account(balances, "Olya", 713) + +total_pence = sum_balances(balances) +total_string = format_pence_as_string(total_pence) + +print(f"The bank accounts total {total_string}") diff --git a/sprint5-prep/prep3-exercise.py b/sprint5-prep/prep3-exercise.py new file mode 100644 index 00000000..9a72025e --- /dev/null +++ b/sprint5-prep/prep3-exercise.py @@ -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) + + +eliza = Person("Eliza", 34, "Arch Linux") +print(eliza.name) + + +def is_adult(person: Person) -> bool: + return person.age >= 18 + + +print(is_adult(imran)) + + +def is_from_UK(person: Person) -> bool: + return person.country == "UK" + + +print(is_from_UK(eliza)) diff --git a/sprint5-prep/prep4-exercise.py b/sprint5-prep/prep4-exercise.py new file mode 100644 index 00000000..51f2ee32 --- /dev/null +++ b/sprint5-prep/prep4-exercise.py @@ -0,0 +1,22 @@ +import datetime + + +class Person: + def __init__( + self, name: str, date_of_birth: datetime.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(self): + today = datetime.date.today() + age = today.year - self.date_of_birth.year + print(age) + return age >= 18 + + +imran = Person("Imran", datetime.date(1998, 4, 3), "Ubuntu") +print(imran.is_adult()) +eliza = Person("Eliza", datetime.date(2013, 3, 17), "Arch Linux") +print(eliza.is_adult()) diff --git a/sprint5-prep/prep5-exercise.py b/sprint5-prep/prep5-exercise.py new file mode 100644 index 00000000..3766edeb --- /dev/null +++ b/sprint5-prep/prep5-exercise.py @@ -0,0 +1,24 @@ +from dataclasses import dataclass +import datetime + + +@dataclass(frozen=True) +class Person: + name: str + date_of_birth: datetime.date + preferred_operating_system: str + + def is_adult(self) -> bool: + today = datetime.date.today() + age = today.year - self.date_of_birth.year + if (today.month, today.day) < ( + self.date_of_birth.month, + self.date_of_birth.day, + ): + age -= 1 + return age >= 18 + + +imran = Person("Imran", datetime.date(2007, 12, 16), "Ubuntu") +print(imran) +print(imran.is_adult()) diff --git a/sprint5-prep/prep6-exercise.py b/sprint5-prep/prep6-exercise.py new file mode 100644 index 00000000..ea02d922 --- /dev/null +++ b/sprint5-prep/prep6-exercise.py @@ -0,0 +1,24 @@ +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=8, children=[]) +aisha = Person(name="Aisha", age=12, 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})") + + +print_family_tree(imran) diff --git a/sprint5-prep/prep7-exercise.py b/sprint5-prep/prep7-exercise.py new file mode 100644 index 00000000..7645f71c --- /dev/null +++ b/sprint5-prep/prep7-exercise.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}") diff --git a/sprint5-prep/prep8-exercise.py b/sprint5-prep/prep8-exercise.py new file mode 100644 index 00000000..144b0e9e --- /dev/null +++ b/sprint5-prep/prep8-exercise.py @@ -0,0 +1,126 @@ +from dataclasses import dataclass +from enum import Enum +from typing import List +import sys +from collections import Counter + + +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 + + +name = input("Insert your name :") +if len(name) == 0: + print("Name cannot be empty!", file=sys.stderr) + exit(1) +elif len(name) > 50: + print("Name is too long!", file=sys.stderr) + exit(1) +try: + age = int(input("Insert your age :")) + if age < 18 or age > 150: + raise ValueError("Age out of range") +except ValueError as e: + print(f"Invalid age: {e}", file=sys.stderr) + sys.exit(1) +preferred_os = input( + "Insert your preferred_operating_system(macOS,Arch Linux,Ubuntu) :" +) +try: + preferred_operating_system = OperatingSystem(preferred_os) +except ValueError: + print("error in os name", file=sys.stderr) + sys.exit(1) + +person = Person(name, age, preferred_operating_system) + + +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, + ), + Laptop( + id=5, + manufacturer="Dell", + model="XPS", + screen_size_in_inches=13, + operating_system=OperatingSystem.ARCH, + ), +] + +os_count = Counter(laptop.operating_system for laptop in laptops) +max_count = max(os_count.values()) + +print(f"\nHello {person.name}, here is the laptop availability info:") +print(f"You have requested: {person.preferred_operating_system.value}") + +# print(max_count) +most_available_os = [] +for os in os_count: + count = os_count[os] + if count == max_count: + most_available_os.append(os.value) +# print(most_available_os) +possible_laptops = find_possible_laptops(laptops, person) +print( + f"Count of available {person.preferred_operating_system.value} laptops for you is : {len(possible_laptops)}" +) + +formatted_os = [f"{os.value} {os_count[os]}" for os in os_count if os_count[os] == max_count] +print(f"Most available operating systems: {', '.join(formatted_os)}") + +if len(possible_laptops) < max_count: + print( + f"Note: If you are willing to accept : {" or ".join(most_available_os)}, you are more likely to get a laptop." + ) diff --git a/sprint5-prep/prep9-exercise.py b/sprint5-prep/prep9-exercise.py new file mode 100644 index 00000000..e9bd3fb7 --- /dev/null +++ b/sprint5-prep/prep9-exercise.py @@ -0,0 +1,38 @@ +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: list[str] = [] + + 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())