diff --git a/.gitignore b/.gitignore index 3c3629e6..274d0491 100644 --- a/.gitignore +++ b/.gitignore @@ -1 +1,2 @@ node_modules +.venv \ No newline at end of file diff --git a/sprint-5/prep-exercises/Enumeration.py b/sprint-5/prep-exercises/Enumeration.py new file mode 100644 index 00000000..f904154d --- /dev/null +++ b/sprint-5/prep-exercises/Enumeration.py @@ -0,0 +1,82 @@ +#Exercise: Enumeration in Python +# Write a program which: + +# Already has a list of Laptops that a library has to lend out. +# Accepts user input to create a new Person - it should use the input function to read a person’s name, age, and preferred operating system. +# Tells the user how many laptops the library has that have that operating system. +# If there is an operating system that has more laptops available, tells the user that if they’re willing to accept that operating system they’re more likely to get a laptop. +# You should convert the age and preferred operating system input from the user into more constrained types as quickly as possible, and should output errors to stderr and terminate the program with a non-zero exit code if the user input bad values. + +#solution: + +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 + +laptops = [ + Laptop(1, "Dell", "XPS", 13, operating_system=OperatingSystem.ARCH), + Laptop(2, "Dell", "XPS", 15, operating_system=OperatingSystem.UBUNTU), + Laptop(3, "Dell", "XPS", 15, operating_system=OperatingSystem.UBUNTU), + Laptop(4, "Apple","macBook", 13, operating_system=OperatingSystem.MACOS), +] + +def convert_age(age_str: str) -> int: + if not age_str.isdigit(): + print("Error: Age must be a positive integer.", file=sys.stderr) + sys.exit(1) + return int(age_str) + +def count_laptops_by_os(laptops: List[Laptop]): + counts ={os_enum: 0 for os_enum in OperatingSystem} + for laptop in laptops: + counts[laptop.operating_system] += 1 + return counts + +def main(): + name = input("Enter your name: ").strip() + age_input = input("Enter your age: ").strip() + preferred_os_str = input("Enter your preferred operating system (macOS, Arch Linux, Ubuntu): ").strip() + age = convert_age(age_input) + + try: + preferred_os = OperatingSystem(preferred_os_str) + except ValueError: + print(f"Error: '{preferred_os_str}' is not a valid operating system.", file=sys.stderr) + sys.exit(1) + + person = Person(name=name, age=age, preferred_operating_system=preferred_os) + + os_counts = count_laptops_by_os(laptops) + preferred_os_count = os_counts[person.preferred_operating_system] + + print(f"There are {preferred_os_count} laptops available with {person.preferred_operating_system.value}.") + + max_os = max(os_counts, key=os_counts.get) + best_count = os_counts[max_os] + + if max_os != person.preferred_operating_system and best_count > preferred_os_count: + print(f"If you're willing to accept {max_os.value}, there are {os_counts[max_os]} laptops available." + f" you would have more laptop options ({best_count} available).") +if __name__ == "__main__": + main() + diff --git a/sprint-5/prep-exercises/class-and-objects.py b/sprint-5/prep-exercises/class-and-objects.py new file mode 100644 index 00000000..35cac903 --- /dev/null +++ b/sprint-5/prep-exercises/class-and-objects.py @@ -0,0 +1,49 @@ + # 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) + +#exercise 1: +#Read the error, and make sure you understand what it’s telling you. + +#solution: +# The error message indicates that the Person class does not have an attribute named "address". +# This means that when the code tries to access eliza.address and imran.address, it fails because the address attribute was never defined in the Person class. + # class-and-objects.py:9: error: "Person" has no attribute "address" [attr-defined] + # class-and-objects.py:13: error: "Person" has no attribute "address" [attr-defined] + # Found 2 errors in 1 file (checked 1 source file) + +#exercise 2: +# Add the is_adult code to the file you saved earlier. +# Run it through mypy - notice that no errors are reported - mypy understands that Person has a property named age so is happy with the function. +# Write a new function in the file that accepts a Person as a parameter and tries to access a property that doesn’t exist. Run it through mypy and check that it does report an error. + +#solution: + +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 get_favorite_color(person: Person) -> str: + return person.favorite_color + +# When I run mypy on this code, it shows an error related to the get_favorite_color function. + # class-and-objects.py:42: error: Returning Any from function declared to return "str" [no-any-return] + # class-and-objects.py:42: error: "Person" has no attribute "favorite_color" [attr-defined] + # Found 2 errors in 1 file (checked 1 source file) +# The error message indicates that the Person class does not have an attribute named "favorite_color". +# This means that when the code tries to access person.favorite_color, it fails because the favorite_color attribute was never defined in the Person class. \ No newline at end of file diff --git a/sprint-5/prep-exercises/data-classes.py b/sprint-5/prep-exercises/data-classes.py new file mode 100644 index 00000000..654dd9b6 --- /dev/null +++ b/sprint-5/prep-exercises/data-classes.py @@ -0,0 +1,23 @@ +#Exercise 1: +# Write a Person class using @datatype which uses a datetime.date for date of birth, rather than an int for age. +# Re-add the is_adult method to it. + +#solution: + +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: + today = date.today() + age = today.year - self.date_of_birth.year - ((today.month, today.day) < ( + self.date_of_birth.month, self.date_of_birth.day) + ) + return age >= 18 +imran = Person("Imran", date(2001, 5, 15), "Ubuntu") +print(imran.name) +print(imran.is_adult()) \ No newline at end of file diff --git a/sprint-5/prep-exercises/double.py b/sprint-5/prep-exercises/double.py new file mode 100644 index 00000000..40eae280 --- /dev/null +++ b/sprint-5/prep-exercises/double.py @@ -0,0 +1,33 @@ +def double(value): + return value * 2 + +print(double("22")) + +#exercise 1: +# Predict what double("22") will do. Then run the code and check. Did it do what you expected? Why did it return the value it did? + +#solution: +# My prediction is that it will return "44" but +# when I run the code I got "2222" which is different from my prediction +# because the function takes the string "22" concatenates it with itself, resulting in "2222". When you multiply a string by an integer in Python, it repeats the string that many times. + + + +#exercise 2: +#Read the code and write down what the bug is. How would you fix it? +def double(number): + return number * 3 + +print(double(10)) + +#solution: +# The bug is that the function is supposed to double the input value, but it is actually tripling it by multiplying by 3. +# To fix it, I would change the multiplication factor from 3 to 2, like this: + +# def double(number): +# return number * 2 or + +# I would rename the function to reflect its actual behavior: + +# def triple(number): +# return number * 3 diff --git a/sprint-5/prep-exercises/generics.py b/sprint-5/prep-exercises/generics.py new file mode 100644 index 00000000..425767d0 --- /dev/null +++ b/sprint-5/prep-exercises/generics.py @@ -0,0 +1,20 @@ +#exercise-1 +#Fix the below code so that it works. You must not change the print on line 20 - we do want to print the children’s ages. (Feel free to invent the ages of Imran’s children.) + +#solution: +# +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=5, children=[]) +aisha = Person(name="Aisha", age=7, children=[]) +imran = Person(name="Imran", age=30, 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/sprint-5/prep-exercises/inheritance.py b/sprint-5/prep-exercises/inheritance.py new file mode 100644 index 00000000..1e7161b3 --- /dev/null +++ b/sprint-5/prep-exercises/inheritance.py @@ -0,0 +1,49 @@ +#Exercise: +#Play computer with this code. Predict what you expect each line will do. Then run the code and check your predictions. (If any lines cause errors, you may need to comment them out to check later lines). +#Solution: +# The first four print statements will work as expected, showing the child's name before and after the last name change. +# The last four print statements will cause errors because the Parent class does not have the get_full_name method or the change_last_name method. +# To fix the errors, we can comment out the lines that call these methods on the Parent instance.or we can implement these methods in the Parent class if needed. + +class Parent: + def __init__(self, first_name: str, last_name: str): + self.first_name = first_name + self.last_name = last_name + self.previous_last_names = [] + + def get_name(self) -> str: + return f"{self.first_name} {self.last_name}" + + 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 self.previous_last_names: + suffix = f" (née {self.previous_last_names[0]})" + return f"{self.first_name} {self.last_name}{suffix}" + + +class Child(Parent): + + def __init__(self, first_name: str, last_name: str): + super().__init__(first_name, last_name) + self.nickname = None + + +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()) # + + diff --git a/sprint-5/prep-exercises/methods.py b/sprint-5/prep-exercises/methods.py new file mode 100644 index 00000000..fa3a45b8 --- /dev/null +++ b/sprint-5/prep-exercises/methods.py @@ -0,0 +1,35 @@ +#exercise 1: +# Think of the advantages of using methods instead of free functions. Write them down in your notebook. + +#solution: +# 1. Encapsulation: Methods allow for better encapsulation of data and behavior within a class, making it easier to manage and maintain code. +# 2. Code Organization: Methods help organize code by grouping related functionality together within a class, improving readability and structure. +# 3. Inheritance and Polymorphism: Methods can be overridden in subclasses, allowing for polymorphic behavior and code reuse through inheritance. +# 4. Access to Instance Data: Methods have access to the instance's data (attributes), enabling them to operate on the object's state directly. +# 5. Namespace Management: Methods help avoid naming conflicts by being scoped within the class, reducing the likelihood of global namespace pollution. +# 6. Improved Collaboration: In team environments, methods within classes can provide clear interfaces for collaboration, making it easier for multiple developers to work on the same codebase. +# 7. Object-Oriented Design: Methods are a fundamental part of object-oriented programming, promoting principles like encapsulation, abstraction, and modularity. + +#exercise 2: +#Change the Person class to take a date of birth (using the standard library’s datetime.date class) and store it in a field instead of age. +#Update the is_adult method to act the same as before. + +#solution: + +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(self) -> bool: + today = date.today() + age = today.year - self.date_of_birth.year - ((today.month, today.day) < ( + self.date_of_birth.month, self.date_of_birth.day) + ) + return age >= 18 + +imran = Person("Imran", date(2001, 5, 15), "Ubuntu") +print(imran.name) +print(imran.is_adult()) \ No newline at end of file diff --git a/sprint-5/prep-exercises/type-checking.py b/sprint-5/prep-exercises/type-checking.py new file mode 100644 index 00000000..fa03338c --- /dev/null +++ b/sprint-5/prep-exercises/type-checking.py @@ -0,0 +1,77 @@ +# This code contains bugs related to types. They are bugs mypy can catch. + +# Read this code to understand what it’s trying to do. Add type annotations to the method parameters and return types of this code. Run the code through mypy, and fix all of the bugs that show up. When you’re confident all of the type annotations are correct, and the bugs are fixed, run the code and check it works. + + # def open_account(balances, name, amount): + # balances[name] = amount + + # def sum_balances(accounts): + # 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): + # 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}") + +#when I run mypy on this code, it shows several type errors related to the parameters passed to the open_account function. + # type-checking.py:5: error: Function is missing a type annotation [no-untyped-def] + # type-checking.py:8: error: Function is missing a type annotation [no-untyped-def] + # type-checking.py:15: error: Function is missing a type annotation [no-untyped-def] + # type-checking.py:28: error: Call to untyped function "open_account" in typed context [no-untyped-call] + # type-checking.py:28: error: Missing positional argument "amount" in call to "open_account" [call-arg] + # type-checking.py:29: error: Call to untyped function "open_account" in typed context [no-untyped-call] + # type-checking.py:29: error: Missing positional argument "amount" in call to "open_account" [call-arg] + # type-checking.py:31: error: Call to untyped function "sum_balances" in typed context [no-untyped-call] + # type-checking.py:32: error: Name "format_pence_as_str" is not defined [name-defined] + # Found 9 errors in 1 file (checked 1 source file) +#To fix these errors, In the open_account function I need to ensure that the balances parameter is a dictionary mapping strings to integers, the name parameter is a string, and the amount parameter is an integer . +#Additionally, I need to make sure that the values passed to the open_account function are of the correct types. +#Here is the corrected code with type annotations and fixed function calls: + +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 = int(total_pence / 100) + pence = total_pence % 100 + return f"£{pounds}.{pence:02d}" +balances: Dict[str, int] = { + "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}") + +# Now, when I run mypy on the corrected code, I get no type errors, indicating that all type annotations are correct and the bugs have been fixed.with the result:"# Success: no issues found in 1 source file" \ No newline at end of file diff --git a/sprint-5/prep-exercises/type-guided.py b/sprint-5/prep-exercises/type-guided.py new file mode 100644 index 00000000..8f3ff5ca --- /dev/null +++ b/sprint-5/prep-exercises/type-guided.py @@ -0,0 +1,48 @@ +#exercise : +# Try changing the type annotation of Person.preferred_operating_system from str to List[str]. +# Run mypy on the code. +# It tells us different places that our code is now wrong, because we’re passing values of the wrong type. +# We probably also want to rename our field - lists are plural. Rename the field to preferred_operating_systems. +# Run mypy again. +# Fix all of the places that mypy tells you need changing. Make sure the program works as you’d expect. + + + #solution: + #when I change the str to List[str] and run mypy it shows the following errors: + # type-guided.py:31: error: "Person" has no attribute "preferred_operating_system"; maybe "preferred_operating_systems"? [attr-defined] + # type-guided.py:37: error: Unexpected keyword argument "preferred_operating_system" for "Person"; did you mean "preferred_operating_systems"? [call-arg] + # type-guided.py:38: error: Unexpected keyword argument "preferred_operating_system" for "Person"; did you mean "preferred_operating_systems"? +# then I rename the field to preferred_operating_systems and run mypy again, it shows no errors. +# Here is the corrected code: + +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"), +] + \ No newline at end of file