From ef6724cb43a9df21e0eceb61ec44e4589d2d6b32 Mon Sep 17 00:00:00 2001 From: Aliaksandr Yakutovich Date: Fri, 5 May 2023 11:08:29 +0200 Subject: [PATCH 1/2] Fix exercise 1 on objects. --- object_oriented_programming.ipynb | 24 +++++++++---------- .../tests/test_object_oriented_programming.py | 19 ++++++++++++--- 2 files changed, 27 insertions(+), 16 deletions(-) diff --git a/object_oriented_programming.ipynb b/object_oriented_programming.ipynb index d24c841d..1eb5fdc3 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,9 +1079,15 @@ "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. The output should be `Ice cream scoop with flavor ''`, where `'`, where `\n", "

Question

\n", @@ -1104,9 +1109,6 @@ " \"\"\"A class representing a single scoop of ice cream\"\"\"\n", " # Write your class implementation here\n", "\n", - "\n", - "flavors = # TODO: create a tuple containing the flavors\n", - "\n", "def solution_ice_cream_scoop(flavors: tuple[str]) -> list[str]:\n", " # Write your solution here\n", " pass" @@ -1116,7 +1118,6 @@ "cell_type": "markdown", "id": "824ca183-251f-47bc-9e47-a780872c5ecf", "metadata": { - "jp-MarkdownHeadingCollapsed": true, "tags": [] }, "source": [ @@ -1128,7 +1129,8 @@ "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.\n", + "You *should use* the custom 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", @@ -1162,9 +1164,6 @@ "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", " # Write your solution here\n", @@ -1175,7 +1174,6 @@ "cell_type": "markdown", "id": "419b5378-ff17-4ed3-8203-97c132918cb7", "metadata": { - "jp-MarkdownHeadingCollapsed": true, "tags": [] }, "source": [ @@ -1545,7 +1543,7 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", - "version": "3.11.0" + "version": "3.11.1" } }, "nbformat": 4, diff --git a/tutorial/tests/test_object_oriented_programming.py b/tutorial/tests/test_object_oriented_programming.py index 52a9eea5..4705214f 100644 --- a/tutorial/tests/test_object_oriented_programming.py +++ b/tutorial/tests/test_object_oriented_programming.py @@ -6,6 +6,15 @@ 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 # @@ -20,10 +29,14 @@ def __init__(self, flavor: str): def __str__(self): return f"Ice cream scoop with flavor '{self.flavor}'" +def reference_ice_cream_scoop(flavors: tuple[str]) -> list[Scoop, str]: + return [(Scoop(flavor), str(Scoop(flavor))) for flavor in flavors] -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] +@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 # From bf21c9c5d47a03bf72a03cf420a34aca288d8d60 Mon Sep 17 00:00:00 2001 From: edoardob90 Date: Fri, 5 May 2023 18:59:55 +0200 Subject: [PATCH 2/2] Fixes exercise text and solution skeleton --- object_oriented_programming.ipynb | 37 +++++++++---------- .../tests/test_object_oriented_programming.py | 11 +++--- 2 files changed, 24 insertions(+), 24 deletions(-) diff --git a/object_oriented_programming.ipynb b/object_oriented_programming.ipynb index 1eb5fdc3..9e4be5bc 100644 --- a/object_oriented_programming.ipynb +++ b/object_oriented_programming.ipynb @@ -1083,7 +1083,7 @@ "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 `'`, where `` is the actual scoop's flavor.\n", "**Pay attention to the single quotes!**\n", "\n", "Modify the `solution_ice_cream_scop` function to return a list of tuples each containing a Scoop instance and its flavor.\n", @@ -1091,7 +1091,7 @@ "\n", "
\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", "
" ] }, @@ -1105,13 +1105,15 @@ "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", - "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" ] }, { @@ -1129,26 +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.\n", - "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", + "" ] }, { @@ -1161,19 +1156,22 @@ "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", - "def solution_ice_cream_bowl(flavors: tuple[str]) -> str:\n", + "\n", + "\n", + "def solution_ice_cream_bowl(flavors: tuple[str]) -> tuple[object, str]:\n", " # Write your solution here\n", - " pass" + " return" ] }, { "cell_type": "markdown", "id": "419b5378-ff17-4ed3-8203-97c132918cb7", "metadata": { + "jp-MarkdownHeadingCollapsed": true, "tags": [] }, "source": [ @@ -1293,6 +1291,7 @@ "cell_type": "markdown", "id": "09ef6028-b6a6-4f79-a418-bce3e3e3f60f", "metadata": { + "jp-MarkdownHeadingCollapsed": true, "tags": [] }, "source": [ @@ -1543,7 +1542,7 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", - "version": "3.11.1" + "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 4705214f..147da554 100644 --- a/tutorial/tests/test_object_oriented_programming.py +++ b/tutorial/tests/test_object_oriented_programming.py @@ -7,13 +7,12 @@ FLAVORS = [ - (), - ("chocolate"), + ("chocolate",), ("chocolate", "vanilla", "persimmon"), ("chocolate", "vanilla", "stracciatella"), ("chocolate", "vanilla", "stracciatella", "strawberry"), ("chocolate", "vanilla", "stracciatella", "strawberry", "pistachio"), - ] +] # # Exercise 1: Ice cream scoop @@ -29,9 +28,11 @@ def __init__(self, flavor: str): def __str__(self): return f"Ice cream scoop with flavor '{self.flavor}'" + 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)] @@ -75,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() @@ -185,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: