diff --git a/object_oriented_programming.ipynb b/object_oriented_programming.ipynb index d24c841d..9e4be5bc 100644 --- a/object_oriented_programming.ipynb +++ b/object_oriented_programming.ipynb @@ -1068,7 +1068,6 @@ "cell_type": "markdown", "id": "a2cc7735-1d30-47b3-a172-96201e26f98d", "metadata": { - "jp-MarkdownHeadingCollapsed": true, "tags": [] }, "source": [ @@ -1080,13 +1079,19 @@ "id": "94f8c9c0-0f4e-4452-88eb-62ff87d218e9", "metadata": {}, "source": [ - "Define a class `Scoop` that represents a single scoop of ice cream. Each scoop should have a **single** attribute, `flavor`, a string that you can initialize when you create the instance of `Scoop`.\n", + "Define a class `Scoop` that represents a single scoop of ice cream.\n", + "Each scoop should have a **single** attribute, `flavor`, a string that you can initialize when you create the instance of `Scoop`.\n", + "\n", + "Define also a `__str__` method to return a string reprensentation of a scoop.\n", + "The output should be `Ice cream scoop with flavor ''`, where `` is the actual scoop's flavor.\n", + "**Pay attention to the single quotes!**\n", "\n", - "Define also a `__str__` method to return a string reprensentation of a scoop. The output should be `Ice cream scoop with flavor ''`, where `\n", "

Question

\n", - " Complete the solution function such that it creates three instances of the Scoop class with the following flavors: chocolate, vanilla, persimmon. This function should return a list that collects the string representations of the ice cream scoops.\n", + " Complete the solution function to create instances of the Scoop class with the following example flavors: chocolate, vanilla, persimmon. This function should return a list of tuples of the kind (Scoop, __str__), where __str__ stands for the string representations of the ice cream Scoop instance.\n", "" ] }, @@ -1100,23 +1105,21 @@ "outputs": [], "source": [ "%%ipytest\n", + "\n", "class Scoop:\n", " \"\"\"A class representing a single scoop of ice cream\"\"\"\n", " # Write your class implementation here\n", + " \n", "\n", - "\n", - "flavors = # TODO: create a tuple containing the flavors\n", - "\n", - "def solution_ice_cream_scoop(flavors: tuple[str]) -> list[str]:\n", + "def solution_ice_cream_scoop(flavors: tuple[str]) -> list[tuple]:\n", " # Write your solution here\n", - " pass" + " return" ] }, { "cell_type": "markdown", "id": "824ca183-251f-47bc-9e47-a780872c5ecf", "metadata": { - "jp-MarkdownHeadingCollapsed": true, "tags": [] }, "source": [ @@ -1128,25 +1131,19 @@ "id": "4244ff19-7bbf-43cc-9d1e-eba13f7f5e4e", "metadata": {}, "source": [ - "Create a class `Bowl` that can hold many ice cream scoops, as many as you like. You *should use* the custom class you created in the previous exercise.\n", + "Create a class `Bowl` that can hold many ice cream scoops, as many as you like. You should use the `Scoop` class you created in the previous exercise.\n", "\n", "The `Bowl` class should have a method called `add_scoops()` that accept **variable number** of scoops.\n", "\n", "
\n", "

Hint

\n", - " In the __init__ method of the Bowl class, you should define an attribute that acts as a container to hold the scoops you might want to add\n", + " In the __init__ method of the Bowl class, you should define an attribute that acts as a container to hold all the scoops you might want to add\n", "
\n", "\n", "
\n", "

Question

\n", - " Complete the solution function that creates a bowl of three scoops with flavors chocolate, vanilla, and stracciatella. The output of this function should be a string that reports the content of the bowl just created.\n", - "
\n", - "\n", - "For example:\n", - "\n", - "```\n", - "Ice cream bowl with chocolate, vanilla, stracciatella scoops\n", - "```" + " Complete the solution function to create a bowl of scoops with flavors chocolate, vanilla, and stracciatella, for example. The function should output a tuple containing the Bowl instance and its content (a string). The bowl content should be formatted as follows: Ice cream bowl with chocolate, vanilla, stracciatella scoops\n", + "" ] }, { @@ -1159,16 +1156,15 @@ "outputs": [], "source": [ "%%ipytest\n", + "\n", "class Bowl:\n", " \"\"\"A class representing a bowl of ice cream scoops\"\"\"\n", " # Write your class implementation here\n", "\n", "\n", - "flavors = # TODO: create a tuple containing the flavors\n", - " \n", - "def solution_ice_cream_bowl(flavors: tuple[str]) -> str:\n", + "def solution_ice_cream_bowl(flavors: tuple[str]) -> tuple[object, str]:\n", " # Write your solution here\n", - " pass" + " return" ] }, { @@ -1295,6 +1291,7 @@ "cell_type": "markdown", "id": "09ef6028-b6a6-4f79-a418-bce3e3e3f60f", "metadata": { + "jp-MarkdownHeadingCollapsed": true, "tags": [] }, "source": [ @@ -1545,7 +1542,7 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", - "version": "3.11.0" + "version": "3.11.3" } }, "nbformat": 4, diff --git a/tutorial/tests/test_object_oriented_programming.py b/tutorial/tests/test_object_oriented_programming.py index 52a9eea5..147da554 100644 --- a/tutorial/tests/test_object_oriented_programming.py +++ b/tutorial/tests/test_object_oriented_programming.py @@ -6,6 +6,14 @@ from numpy import average +FLAVORS = [ + ("chocolate",), + ("chocolate", "vanilla", "persimmon"), + ("chocolate", "vanilla", "stracciatella"), + ("chocolate", "vanilla", "stracciatella", "strawberry"), + ("chocolate", "vanilla", "stracciatella", "strawberry", "pistachio"), +] + # # Exercise 1: Ice cream scoop # @@ -21,9 +29,15 @@ def __str__(self): return f"Ice cream scoop with flavor '{self.flavor}'" -def test_ice_cream_scoop(function_to_test) -> None: - flavors = ("chocolate", "vanilla", "persimmon") - assert function_to_test(flavors) == [str(Scoop(flavor)) for flavor in flavors] +def reference_ice_cream_scoop(flavors: tuple[str]) -> list[Scoop, str]: + return [(Scoop(flavor), str(Scoop(flavor))) for flavor in flavors] + + +@pytest.mark.parametrize("flavors", FLAVORS) +def test_ice_cream_scoop(flavors, function_to_test) -> None: + test_solution = [string for _, string in function_to_test(flavors)] + reference_solution = [string for _, string in reference_ice_cream_scoop(flavors)] + assert test_solution == reference_solution # @@ -62,7 +76,7 @@ def read_data(name: str, data_dir: str = "data") -> pathlib.Path: """Read input data""" current_module = sys.modules[__name__] return ( - pathlib.Path(current_module.__file__).parent / f"{data_dir}/{name}" + pathlib.Path(current_module.__file__).parent / f"{data_dir}/{name}" ).resolve() @@ -172,7 +186,7 @@ def __init__(self, universe_start: str) -> None: def evolve(self) -> "Universe": """Evolve the universe""" for n, moon_i in enumerate(self.moons[:-1]): - for moon_j in self.moons[n + 1:]: + for moon_j in self.moons[n + 1 :]: moon_i.update_velocities(moon_j) for moon in self.moons: