diff --git a/.gitignore b/.gitignore index 3c3629e64..2ab7bcc2b 100644 --- a/.gitignore +++ b/.gitignore @@ -1 +1,4 @@ node_modules +.venv/ +__pycache__/ +*.pyc \ No newline at end of file diff --git a/sprint5-prep-exercise/Why_we_use_types.py b/sprint5-prep-exercise/Why_we_use_types.py new file mode 100644 index 000000000..e61701b9d --- /dev/null +++ b/sprint5-prep-exercise/Why_we_use_types.py @@ -0,0 +1,12 @@ +def double(value): return value * 2 + + + +def fix_double(value): + return int(value) * 2 + +print(double("22")) +print(fix_double("22")) + +#it returns "2222" -|> * works with string and it repeats it but "/" does not work with string +# fix_double returns 44 becouse we converted the string to number \ No newline at end of file diff --git a/sprint5-prep-exercise/classes_and_objects.py b/sprint5-prep-exercise/classes_and_objects.py new file mode 100644 index 000000000..c5d333d91 --- /dev/null +++ b/sprint5-prep-exercise/classes_and_objects.py @@ -0,0 +1,24 @@ +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 + +def print_email(person: Person) -> None: + return person.email + +imran = Person("Imran", 22, "Ubuntu") +print(imran.name) +#print(imran.address) doesnt worrk becuase there no address atribute + +eliza = Person("Eliza", 34, "Arch Linux") +print(eliza.name) +#print(eliza.address) doesnt worrk becuase there no address atribute + +print(is_adult(imran)) + +#print(print_email(imran)) there is no email atribute to print \ No newline at end of file diff --git a/sprint5-prep-exercise/dataclasses_person.py b/sprint5-prep-exercise/dataclasses_person.py new file mode 100644 index 000000000..ebbf4a2bd --- /dev/null +++ b/sprint5-prep-exercise/dataclasses_person.py @@ -0,0 +1,25 @@ +from dataclasses import dataclass +from datetime import date + +@dataclass(frozen=True) +class Person: + name: str + dob: date + preferred_operating_system: str + + def is_adult(self) -> bool: + today = date.today() + + age = today.year - self.dob.year + + # adjust if birthday hasn’t happened yet this year + if (today.month, today.day) < (self.dob.month, self.dob.day): + age -= 1 + + return age >= 18 + + +imran = Person("Imran", date(2003, 5, 10), "Ubuntu") + +print(imran) +print(imran.is_adult()) \ No newline at end of file diff --git a/sprint5-prep-exercise/double.py b/sprint5-prep-exercise/double.py new file mode 100644 index 000000000..16c073051 --- /dev/null +++ b/sprint5-prep-exercise/double.py @@ -0,0 +1,10 @@ +def double(number): + return number * 3 + +def fix_double(number): + return number * 2 + +print(double(10)) +print(fix_double(10)) + +#it wants double so we multiply by 2 \ No newline at end of file diff --git a/sprint5-prep-exercise/enums.py b/sprint5-prep-exercise/enums.py new file mode 100644 index 000000000..31e9195d5 --- /dev/null +++ b/sprint5-prep-exercise/enums.py @@ -0,0 +1,78 @@ +from dataclasses import dataclass +from enum import Enum +from typing import List +import sys + +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), +] + + +name = input("Enter your name: ") + +try: + age = int(input("Enter your age: ")) +except ValueError: + print("Invalid age. Must be number!", file=sys.stderr) + sys.exit(1) + +os_input = input("Enter preferred OS (macOS / Arch Linux / Ubuntu): ") + +try: + preferred_os = OperatingSystem(os_input) +except ValueError: + print("Invalid operating system.", file=sys.stderr) + sys.exit(1) + +person = Person(name=name, age=age, preferred_operating_system=preferred_os) + +matching = find_possible_laptops(laptops, person) + +print(f"\nWe have {len(matching)} laptop(s) with {person.preferred_operating_system.value}.") + +# Compare with other OS availability +os_counts = {} + +for laptop in laptops: + os_counts[laptop.operating_system] = os_counts.get(laptop.operating_system, 0) + 1 + +best_os = max(os_counts, key=os_counts.get) + +if best_os != person.preferred_operating_system: + print( + f"If you're flexible, {best_os.value} has more laptops available " + f"({os_counts[best_os]} total)." + ) diff --git a/sprint5-prep-exercise/generics.py b/sprint5-prep-exercise/generics.py new file mode 100644 index 000000000..33c4c1be3 --- /dev/null +++ b/sprint5-prep-exercise/generics.py @@ -0,0 +1,20 @@ +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=10, children=[]) +aisha = Person(name="Aisha", age=12, children=[]) + +imran = Person(name="Imran", age=50, 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) \ No newline at end of file diff --git a/sprint5-prep-exercise/inheritance.py b/sprint5-prep-exercise/inheritance.py new file mode 100644 index 000000000..c7bdcf228 --- /dev/null +++ b/sprint5-prep-exercise/inheritance.py @@ -0,0 +1,70 @@ +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}" + + + + +# create Child object +person1 = Child("Elizaveta", "Alekseeva") +# expected state: +# first_name = Elizaveta +# last_name = Alekseeva +# previous_last_names = [] + +print(person1.get_name()) +# exxpected: "Elizaveta Alekseeva" + +print(person1.get_full_name()) +# expected: "Elizaveta Alekseeva" +# (no previous last names yet) + +person1.change_last_name("Tyurina") +# Expected state update: +# previous_last_names = ["Alekseeva"] +# last_name = "Tyurina" + +print(person1.get_name()) +# expected: "Elizaveta Tyurina" + +print(person1.get_full_name()) +# expected: "Elizaveta Tyurina (née Alekseeva)" + + + +# Parent object + +person2 = Parent("Elizaveta", "Alekseeva") + +print(person2.get_name()) +# expected: "Elizaveta Alekseeva" + +print(person2.get_full_name()) +# error: Parent has no method get_full_name + +# program stops here + +# these lines will NOT run: +# person2.change_last_name("Tyurina") # AttributeError +# print(person2.get_name()) +# print(person2.get_full_name()) \ No newline at end of file diff --git a/sprint5-prep-exercise/methods.py b/sprint5-prep-exercise/methods.py new file mode 100644 index 000000000..d4bfebcb0 --- /dev/null +++ b/sprint5-prep-exercise/methods.py @@ -0,0 +1,20 @@ + + +from datetime import date + +class Person: + def __init__(self, name: str, dob: date, preferred_operating_system: str): + self.name = name + self.dob = dob + self.preferred_operating_system = preferred_operating_system + + def is_adult(self) -> bool: + today = date.today() + + age = today.year - self.dob.year + + # adjust if birthday hasn’t happened yet this year + if (today.month, today.day) < (self.dob.month, self.dob.day): + age -= 1 + + return age >= 18 \ No newline at end of file diff --git a/sprint5-prep-exercise/type_gides_with_mypy.py b/sprint5-prep-exercise/type_gides_with_mypy.py new file mode 100644 index 000000000..d7e52f2b4 --- /dev/null +++ b/sprint5-prep-exercise/type_gides_with_mypy.py @@ -0,0 +1,34 @@ +from typing import Dict + +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 = total_pence // 100 + pence = total_pence % 100 + return f"£{pounds}.{pence:02d}" + + +balances: Dict[str, int] = { + "Sima": 700, + "Linn": 545, + "Georg": 831, +} + +# ✅ correct calls +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}") \ No newline at end of file diff --git a/sprint5-prep-exercise/type_guided_refactorings.py b/sprint5-prep-exercise/type_guided_refactorings.py new file mode 100644 index 000000000..8e03afa7f --- /dev/null +++ b/sprint5-prep-exercise/type_guided_refactorings.py @@ -0,0 +1,42 @@ +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(1, "Dell", "XPS", 13, "Arch Linux"), + Laptop(2, "Dell", "XPS", 15, "Ubuntu"), + Laptop(3, "Dell", "XPS", 15, "ubuntu"), + Laptop(4, "Apple", "macBook", 13, "macOS"), +] + +for person in people: + possible_laptops = find_possible_laptops(laptops, person) + print(f"Possible laptops for {person.name}: {possible_laptops}") \ No newline at end of file