From 5be1c787a57316a07bc3ff9718239fda8ffdba73 Mon Sep 17 00:00:00 2001 From: Rashaad Ebrahim Date: Wed, 3 Dec 2025 14:36:50 +0200 Subject: [PATCH 01/17] gitignore updated --- .gitignore | 2 ++ 1 file changed, 2 insertions(+) diff --git a/.gitignore b/.gitignore index 3c3629e6..5da303ac 100644 --- a/.gitignore +++ b/.gitignore @@ -1 +1,3 @@ node_modules +**/.venv +**/requirements.txt From ca9bd9e428b2d2a6b6a783b1c489d306a0125b6e Mon Sep 17 00:00:00 2001 From: Rashaad Ebrahim Date: Tue, 23 Dec 2025 18:11:59 +0200 Subject: [PATCH 02/17] gitignore updated --- .gitignore | 2 +- implement-cowsay/requirements.txt | 1 + implement-shell-tools/requirements.txt | 0 3 files changed, 2 insertions(+), 1 deletion(-) create mode 100644 implement-cowsay/requirements.txt create mode 100644 implement-shell-tools/requirements.txt diff --git a/.gitignore b/.gitignore index 5da303ac..3e5a9836 100644 --- a/.gitignore +++ b/.gitignore @@ -1,3 +1,3 @@ node_modules **/.venv -**/requirements.txt + diff --git a/implement-cowsay/requirements.txt b/implement-cowsay/requirements.txt new file mode 100644 index 00000000..c6b9ffd0 --- /dev/null +++ b/implement-cowsay/requirements.txt @@ -0,0 +1 @@ +cowsay diff --git a/implement-shell-tools/requirements.txt b/implement-shell-tools/requirements.txt new file mode 100644 index 00000000..e69de29b From 89b9a00e4f25ce3859af632fa67cf16c70c99af2 Mon Sep 17 00:00:00 2001 From: Rashaad Ebrahim Date: Tue, 6 Jan 2026 18:37:25 +0200 Subject: [PATCH 03/17] Why we use types exercise complete --- prep-exercises/why_we_use_types.py | 24 ++++++++++++++++++++++++ 1 file changed, 24 insertions(+) create mode 100644 prep-exercises/why_we_use_types.py diff --git a/prep-exercises/why_we_use_types.py b/prep-exercises/why_we_use_types.py new file mode 100644 index 00000000..2e3eb94a --- /dev/null +++ b/prep-exercises/why_we_use_types.py @@ -0,0 +1,24 @@ +# def double(number): +# return number * 3 + +# print(double(10)) + + +# --- Exercise --- +# Read the above code and write down what the bug is. How would you fix it? + + +# --- Solution --- +# The bug in the code is that the function is named "double" but actually triples the input number instead of doubling it. + +# To fix it, the multiplication should be changed from 3 to 2 or the naming of the function should change to "triple". + +# Here is the corrected code: + +# def double(number): +# return number * 2 + +# OR + +# def triple(number): +# return number * 3 \ No newline at end of file From 2300312306e57632fefd3c32ebe1c043dca1756e Mon Sep 17 00:00:00 2001 From: Rashaad Ebrahim Date: Tue, 6 Jan 2026 18:44:27 +0200 Subject: [PATCH 04/17] updated function --- prep-exercises/why_we_use_types.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/prep-exercises/why_we_use_types.py b/prep-exercises/why_we_use_types.py index 2e3eb94a..5b3dbd67 100644 --- a/prep-exercises/why_we_use_types.py +++ b/prep-exercises/why_we_use_types.py @@ -15,8 +15,8 @@ # Here is the corrected code: -# def double(number): -# return number * 2 +def double(number): + return number * 2 # OR From 327106becba1a2fc40df3684d671be483ef35279 Mon Sep 17 00:00:00 2001 From: Rashaad Ebrahim Date: Fri, 9 Jan 2026 16:24:03 +0200 Subject: [PATCH 05/17] Why we use types exercises complete --- prep-exercises/why_we_use_types_1a.py | 20 +++++++++++++++++++ ...we_use_types.py => why_we_use_types_1b.py} | 0 2 files changed, 20 insertions(+) create mode 100644 prep-exercises/why_we_use_types_1a.py rename prep-exercises/{why_we_use_types.py => why_we_use_types_1b.py} (100%) diff --git a/prep-exercises/why_we_use_types_1a.py b/prep-exercises/why_we_use_types_1a.py new file mode 100644 index 00000000..44331370 --- /dev/null +++ b/prep-exercises/why_we_use_types_1a.py @@ -0,0 +1,20 @@ +def half(value): + return value / 2 + +def double(value): + return value * 2 + +def second(value): + return value[1] + +print(double("22")) + + +# --- Exercise --- +# 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 double("22") would return an error because "22" is a string and multiplying a string by 2 might not be valid. + +# The function double("22") will return "2222". This is because in Python, multiplying a string by an integer results in the string being repeated that many times. I did also change the * 2 to * 3 to see how it worked. So when we multiply the string "22" by 2, it concatenates "22" with itself, resulting in "2222". diff --git a/prep-exercises/why_we_use_types.py b/prep-exercises/why_we_use_types_1b.py similarity index 100% rename from prep-exercises/why_we_use_types.py rename to prep-exercises/why_we_use_types_1b.py From 967336b299d9703dc477f4aee1bbc837fd5df3fc Mon Sep 17 00:00:00 2001 From: Rashaad Ebrahim Date: Fri, 9 Jan 2026 17:02:25 +0200 Subject: [PATCH 06/17] Type checking with mypy exercise complete --- prep-exercises/type_checking_with_mypy.py | 30 +++++++++++++++++++++++ 1 file changed, 30 insertions(+) create mode 100644 prep-exercises/type_checking_with_mypy.py diff --git a/prep-exercises/type_checking_with_mypy.py b/prep-exercises/type_checking_with_mypy.py new file mode 100644 index 00000000..5c8a3922 --- /dev/null +++ b/prep-exercises/type_checking_with_mypy.py @@ -0,0 +1,30 @@ +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", 9) +open_account(balances, "Olya", 7) + +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 From 882dc1a37a430a8221de186872217aed91ce70ac Mon Sep 17 00:00:00 2001 From: Rashaad Ebrahim Date: Fri, 9 Jan 2026 22:58:17 +0200 Subject: [PATCH 07/17] Classes and objects exercise 1 complete --- prep-exercises/classes_and_objects_1.py | 25 +++++++++++++++++++ ..._use_types_1a.py => why_we_use_types_1.py} | 0 ..._use_types_1b.py => why_we_use_types_2.py} | 0 3 files changed, 25 insertions(+) create mode 100644 prep-exercises/classes_and_objects_1.py rename prep-exercises/{why_we_use_types_1a.py => why_we_use_types_1.py} (100%) rename prep-exercises/{why_we_use_types_1b.py => why_we_use_types_2.py} (100%) diff --git a/prep-exercises/classes_and_objects_1.py b/prep-exercises/classes_and_objects_1.py new file mode 100644 index 00000000..c436919e --- /dev/null +++ b/prep-exercises/classes_and_objects_1.py @@ -0,0 +1,25 @@ + +# 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 --- +# Save the above code to a file, and run it through mypy. + +# Read the error, and make sure you understand what it’s telling you. + +# --- Solution --- +# The error reads as follows: "Person" has no attribute "address" +# This error occurs because we are trying to access an attribute "address" on the Person class, which has not been defined in the class constructor. +# To fix this error, we can either remove the lines that attempt to access the "address" attribute, or we can add an "address" attribute to the Person class constructor. +# self.address = "Unknown" \ No newline at end of file diff --git a/prep-exercises/why_we_use_types_1a.py b/prep-exercises/why_we_use_types_1.py similarity index 100% rename from prep-exercises/why_we_use_types_1a.py rename to prep-exercises/why_we_use_types_1.py diff --git a/prep-exercises/why_we_use_types_1b.py b/prep-exercises/why_we_use_types_2.py similarity index 100% rename from prep-exercises/why_we_use_types_1b.py rename to prep-exercises/why_we_use_types_2.py From 301a2bba7920833abec74e4bed2a9ec6eb5f3523 Mon Sep 17 00:00:00 2001 From: Rashaad Ebrahim Date: Mon, 12 Jan 2026 07:50:15 +0200 Subject: [PATCH 08/17] Classes and objects exercise 2 complete --- prep-exercises/classes_and_objects_2.py | 35 +++++++++++++++++++++++++ 1 file changed, 35 insertions(+) create mode 100644 prep-exercises/classes_and_objects_2.py diff --git a/prep-exercises/classes_and_objects_2.py b/prep-exercises/classes_and_objects_2.py new file mode 100644 index 00000000..1583faf5 --- /dev/null +++ b/prep-exercises/classes_and_objects_2.py @@ -0,0 +1,35 @@ +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 + self.address = "Unknown" + +imran = Person("Imran", 22, "Ubuntu") +print(imran.name) +print(imran.address) + +eliza = Person("Eliza", 34, "Arch Linux") +print(eliza.name) +print(eliza.address) + +def is_adult(person: Person) -> bool: + return person.age >= 18 + +print(is_adult(imran)) + +# Function added that accesses a non-existent property +def is_married(person: Person) -> bool: + return person.married == "Y" or person.married == "y" + +print(is_married(imran)) + +# --- Exercise --- +# 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 --- +# The error I received was: "Person" has no attribute "married" \ No newline at end of file From 817c2ad1c28717a21a947ac880855bd22c58f862 Mon Sep 17 00:00:00 2001 From: Rashaad Ebrahim Date: Mon, 12 Jan 2026 08:31:46 +0200 Subject: [PATCH 09/17] MEthods exercises complete --- prep-exercises/methods_1.py | 8 ++++++++ prep-exercises/methods_2.py | 21 +++++++++++++++++++++ 2 files changed, 29 insertions(+) create mode 100644 prep-exercises/methods_1.py create mode 100644 prep-exercises/methods_2.py diff --git a/prep-exercises/methods_1.py b/prep-exercises/methods_1.py new file mode 100644 index 00000000..74b892ff --- /dev/null +++ b/prep-exercises/methods_1.py @@ -0,0 +1,8 @@ +# --- Exercise --- +# Think of the advantages of using methods instead of free functions. Write them down in your notebook. + +# --- Solution --- +# Methods are bundled with the data they operate on, making the code more organised. +# Methods can be reused across different parts of the program. +# Methods make code more readable and easier to understand. +# Changes to the method's implementation only need to be made in one place. diff --git a/prep-exercises/methods_2.py b/prep-exercises/methods_2.py new file mode 100644 index 00000000..bf302160 --- /dev/null +++ b/prep-exercises/methods_2.py @@ -0,0 +1,21 @@ +# --- Exercise --- +# 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 --- +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(person: Person) -> bool: + today = datetime.date.today() + age = today.year - person.date_of_birth.year + if today.month < person.date_of_birth.month or (today.month == person.date_of_birth.month and today.day < person.date_of_birth.day): + age -= 1 + return age >= 18 + From 455edb06d4e23c104c25c2b69e974b59bf00d776 Mon Sep 17 00:00:00 2001 From: Rashaad Ebrahim Date: Mon, 12 Jan 2026 09:15:02 +0200 Subject: [PATCH 10/17] import from method updated --- prep-exercises/dataclasses.py | 21 +++++++++++++++++++++ prep-exercises/methods_2.py | 6 +++--- 2 files changed, 24 insertions(+), 3 deletions(-) create mode 100644 prep-exercises/dataclasses.py diff --git a/prep-exercises/dataclasses.py b/prep-exercises/dataclasses.py new file mode 100644 index 00000000..92fa312a --- /dev/null +++ b/prep-exercises/dataclasses.py @@ -0,0 +1,21 @@ +# --- Exercise --- +# 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 --- +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(person: Person) -> bool: + today = datetime.date.today() + age = today.year - person.date_of_birth.year + if today.month < person.date_of_birth.month or (today.month == person.date_of_birth.month and today.day < person.date_of_birth.day): + age -= 1 + return age >= 18 + diff --git a/prep-exercises/methods_2.py b/prep-exercises/methods_2.py index bf302160..5363e55c 100644 --- a/prep-exercises/methods_2.py +++ b/prep-exercises/methods_2.py @@ -4,16 +4,16 @@ # Update the is_adult method to act the same as before. # --- Solution --- -import datetime +from datetime import date class Person: - def __init__(self, name: str, date_of_birth: datetime.date, preferred_operating_system: str): + 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(person: Person) -> bool: - today = datetime.date.today() + today = date.today() age = today.year - person.date_of_birth.year if today.month < person.date_of_birth.month or (today.month == person.date_of_birth.month and today.day < person.date_of_birth.day): age -= 1 From f9841155b9cc8d60ce68b76f285b50bed8829a19 Mon Sep 17 00:00:00 2001 From: Rashaad Ebrahim Date: Mon, 12 Jan 2026 09:22:42 +0200 Subject: [PATCH 11/17] dataclass exercise complete --- prep-exercises/dataclasses.py | 22 +++++++++++----------- 1 file changed, 11 insertions(+), 11 deletions(-) diff --git a/prep-exercises/dataclasses.py b/prep-exercises/dataclasses.py index 92fa312a..76ccd5bf 100644 --- a/prep-exercises/dataclasses.py +++ b/prep-exercises/dataclasses.py @@ -4,18 +4,18 @@ # Re-add the is_adult method to it. # --- Solution --- -import datetime +from datetime import date +from dataclasses import dataclass +@dataclass 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 + name: str + date_of_birth: date + preferred_operating_system: str - def is_adult(person: Person) -> bool: - today = datetime.date.today() - age = today.year - person.date_of_birth.year - if today.month < person.date_of_birth.month or (today.month == person.date_of_birth.month and today.day < person.date_of_birth.day): + def is_adult(self) -> bool: + today = date.today() + age = today.year - self.date_of_birth.year + if today.month < self.date_of_birth.month or (today.month == self.date_of_birth.month and today.day < self.date_of_birth.day): age -= 1 - return age >= 18 - + return age >= 18 \ No newline at end of file From fe7f19377a3574a9592936e11320b25358723ae7 Mon Sep 17 00:00:00 2001 From: Rashaad Ebrahim Date: Mon, 12 Jan 2026 09:50:35 +0200 Subject: [PATCH 12/17] Generics exercise complete --- prep-exercises/generics.py | 45 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 45 insertions(+) create mode 100644 prep-exercises/generics.py diff --git a/prep-exercises/generics.py b/prep-exercises/generics.py new file mode 100644 index 00000000..e105a664 --- /dev/null +++ b/prep-exercises/generics.py @@ -0,0 +1,45 @@ +# from dataclasses import dataclass +# from typing import List + +# @dataclass(frozen=True) +# class Person: +# name: str +# children: List["Person"] + +# fatma = Person(name="Fatma", children=[]) +# aisha = Person(name="Aisha", children=[]) + +# imran = Person(name="Imran", 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) + +# --- Exercise --- +# Fix the above code so that it works. You must not change the print on line 17 - 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=12, children=[]) +aisha = Person(name="Aisha",age=10, 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) \ No newline at end of file From 4f8fad941d3e8cf82504a95314b1cf4e88697463 Mon Sep 17 00:00:00 2001 From: Rashaad Ebrahim Date: Mon, 12 Jan 2026 09:53:12 +0200 Subject: [PATCH 13/17] file updated --- prep-exercises/generics.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/prep-exercises/generics.py b/prep-exercises/generics.py index e105a664..96609b06 100644 --- a/prep-exercises/generics.py +++ b/prep-exercises/generics.py @@ -31,10 +31,10 @@ class Person: age:int children: List['Person'] -fatma = Person(name="Fatma",age=12, children=[]) -aisha = Person(name="Aisha",age=10, children=[]) +fatma = Person(name="Fatma",age=13, children=[]) +aisha = Person(name="Aisha",age=6, children=[]) -imran = Person(name="Imran",age=40, children=[fatma, aisha]) +imran = Person(name="Imran",age=42, children=[fatma, aisha]) def print_family_tree(person: Person) -> None: From 0cff3612964e3470d4702a4e943dfad98686de9f Mon Sep 17 00:00:00 2001 From: Rashaad Ebrahim Date: Mon, 12 Jan 2026 12:37:32 +0200 Subject: [PATCH 14/17] Type-guided refactorings exercise complete --- prep-exercises/type-guided_refactoring.py | 56 +++++++++++++++++++++++ 1 file changed, 56 insertions(+) create mode 100644 prep-exercises/type-guided_refactoring.py diff --git a/prep-exercises/type-guided_refactoring.py b/prep-exercises/type-guided_refactoring.py new file mode 100644 index 00000000..beb0fbeb --- /dev/null +++ b/prep-exercises/type-guided_refactoring.py @@ -0,0 +1,56 @@ +# --- 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 --- +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}") \ No newline at end of file From ff969e9874265941298e19dc80daeefe96631e4b Mon Sep 17 00:00:00 2001 From: Rashaad Ebrahim Date: Fri, 16 Jan 2026 13:06:31 +0200 Subject: [PATCH 15/17] Additional OS added to people in type-guided --- prep-exercises/type-guided_refactoring.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/prep-exercises/type-guided_refactoring.py b/prep-exercises/type-guided_refactoring.py index beb0fbeb..cfa5b890 100644 --- a/prep-exercises/type-guided_refactoring.py +++ b/prep-exercises/type-guided_refactoring.py @@ -40,8 +40,8 @@ def find_possible_laptops(laptops: List[Laptop], person: Person) -> List[Laptop] people = [ - Person(name="Imran", age=22, preferred_operating_systems=["Ubuntu"]), - Person(name="Eliza", age=34, preferred_operating_systems=["Arch Linux"]), + Person(name="Imran", age=22, preferred_operating_systems=["Ubuntu", "macOS"]), + Person(name="Eliza", age=34, preferred_operating_systems=["Arch Linux", "Ubuntu"]), ] laptops = [ From 773ecb8fe9afeaac068999840c49b27f73da92d5 Mon Sep 17 00:00:00 2001 From: Rashaad Ebrahim Date: Fri, 16 Jan 2026 13:15:25 +0200 Subject: [PATCH 16/17] enums exercise complete --- prep-exercises/enums.py | 117 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 117 insertions(+) create mode 100644 prep-exercises/enums.py diff --git a/prep-exercises/enums.py b/prep-exercises/enums.py new file mode 100644 index 00000000..af3dcaac --- /dev/null +++ b/prep-exercises/enums.py @@ -0,0 +1,117 @@ +# --- Exercise --- +# Write a program which: + +# 1. Already has a list of Laptops that a library has to lend out. +# 2. 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. +# 3. Tells the user how many laptops the library has that have that operating system. +# 4. 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 + + +def find_possible_laptops(laptops: List[Laptop], person: Person) -> List[Laptop]: + return [ + laptop + for laptop in laptops + if laptop.operating_system == person.preferred_operating_system + ] + + +def read_person_from_input() -> Person: + try: + name = input("Enter name: ").strip() + if not name: + raise ValueError("Name cannot be empty") + + age_input = input("Enter age: ").strip() + age = int(age_input) + if age < 0: + raise ValueError("Age must be non-negative") + + os_input = input( + "Enter preferred operating system (macOS, Arch Linux, Ubuntu): " + ).strip() + + preferred_os = OperatingSystem(os_input) + + return Person( + name=name, + age=age, + preferred_operating_system=preferred_os, + ) + + except ValueError as e: + print(f"Error: {e}", file=sys.stderr) + sys.exit(1) + + +LAPTOPS: List[Laptop] = [ + 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="Lenovo", model="T14 G6", screen_size_in_inches=14, operating_system=OperatingSystem.UBUNTU), + Laptop(id=4, manufacturer="Apple", model="MacBook Air", screen_size_in_inches=13, operating_system=OperatingSystem.MACOS), + Laptop(id=5, manufacturer="Lenovo", model="X1 Carbon", screen_size_in_inches=13, operating_system=OperatingSystem.ARCH), + Laptop(id=6, manufacturer="HP", model="Pavilion", screen_size_in_inches=16, operating_system=OperatingSystem.UBUNTU), + Laptop(id=7, manufacturer="ASUS", model="ExpertBook B1", screen_size_in_inches=14, operating_system=OperatingSystem.ARCH), + Laptop(id=8, manufacturer="Apple", model="MacBook Pro", screen_size_in_inches=14, operating_system=OperatingSystem.MACOS), +] + + +def main() -> None: + person = read_person_from_input() + + matching_laptops = find_possible_laptops(LAPTOPS, person) + print( + f"\nNumber of laptops available with " + f"{person.preferred_operating_system.value}: " + f"{len(matching_laptops)}" + ) + + laptops_per_os: dict[OperatingSystem, int] = {} + for laptop in LAPTOPS: + laptops_per_os[laptop.operating_system] = ( + laptops_per_os.get(laptop.operating_system, 0) + 1 + ) + + most_available_os = max( + laptops_per_os.items(), + key=lambda item: item[1], + )[0] + + if most_available_os != person.preferred_operating_system: + print( + f"If you're willing to accept {most_available_os.value}, " + "you're more likely to get a laptop." + ) + + +if __name__ == "__main__": + main() From 1a4a9e58b6206b651a0722c1f6b060f47aa7e600 Mon Sep 17 00:00:00 2001 From: Rashaad Ebrahim Date: Fri, 16 Jan 2026 18:17:04 +0200 Subject: [PATCH 17/17] Inheritance exercise complete --- prep-exercises/inheritance.py | 81 +++++++++++++++++++++++++++++++++++ 1 file changed, 81 insertions(+) create mode 100644 prep-exercises/inheritance.py diff --git a/prep-exercises/inheritance.py b/prep-exercises/inheritance.py new file mode 100644 index 00000000..03daf798 --- /dev/null +++ b/prep-exercises/inheritance.py @@ -0,0 +1,81 @@ +# --- 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). + +# 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()) + +# 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()) + +# --- Solution --- + +# This defines a class called "Parent" with an initializer that sets the first and last name, and a method to get the full name. +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}" + +# This defines a class called "Child" that inherits from "Parent". It has an initializer that calls the parent's initializer and adds a list that can track previous last names. It also has methods to change the last name and get the full name with any previous last names included. +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()) + +person2 = Parent("Elizaveta", "Alekseeva") +print(person2.get_name()) +print(person2.get_full_name()) # This line will cause an error because the Parent class does not have a get_full_name method. +person2.change_last_name("Tyurina") # This line will cause an error because the Parent class does not have a change_last_name method. +print(person2.get_name()) +print(person2.get_full_name()) # This line will cause an error because the Parent class does not have a get_full_name method.