From 6c64dd0ed43675e30f1fae0d88669c91ecaf56d2 Mon Sep 17 00:00:00 2001 From: SergioAcero Date: Thu, 8 May 2025 14:27:04 +0200 Subject: [PATCH 1/3] fix solution of exercise 3 --- .../tests/test_11_functional_programming.py | 37 +++++++++++-------- 1 file changed, 21 insertions(+), 16 deletions(-) diff --git a/tutorial/tests/test_11_functional_programming.py b/tutorial/tests/test_11_functional_programming.py index 892d3c12..365488f1 100644 --- a/tutorial/tests/test_11_functional_programming.py +++ b/tutorial/tests/test_11_functional_programming.py @@ -90,12 +90,12 @@ def reference_filter_even(my_list: List[int]) -> List[int]: def test_filter_even(function_to_test: Callable, my_list: List[int]): res = function_to_test(my_list) assert isinstance(res, list), "The function you wrote does not return a list" - assert not check_for_loop_in_body(function_to_test), ( - "You are not allowed to use a for loop in this exercise" - ) - assert res == reference_filter_even(my_list), ( - "The list you return is not equal to the expected solution" - ) + assert not check_for_loop_in_body( + function_to_test + ), "You are not allowed to use a for loop in this exercise" + assert res == reference_filter_even( + my_list + ), "The list you return is not equal to the expected solution" # @@ -116,12 +116,12 @@ def reference_add_one(my_list: List[int]) -> List[int]: ], ) def test_add_one(function_to_test: Callable, my_list: List[int]): - assert function_to_test(my_list) == reference_add_one(my_list), ( - "The list you return is not equal to the expected solution" - ) - assert not check_for_loop_in_body(function_to_test), ( - "You are not allowed to use a for loop in this exercise" - ) + assert function_to_test(my_list) == reference_add_one( + my_list + ), "The list you return is not equal to the expected solution" + assert not check_for_loop_in_body( + function_to_test + ), "You are not allowed to use a for loop in this exercise" # @@ -162,7 +162,8 @@ def reference_exercise1(matrix: List[List[int]]) -> List[List[int]]: [(np.eye(3)), (np.random.randint(0, 100, size=(4, 4)))], ) def test_exercise1( - function_to_test: Callable[[List[List[int]]], List[List[int]]], my_input: NDArray + function_to_test: Callable[[List[List[int]]], List[List[int]]], + my_input: NDArray, ): res = function_to_test(my_input.tolist()) assert ( @@ -209,12 +210,14 @@ def reference_exercise3(words: List[str]) -> List[Tuple[str, int]]: return [ (k, len(list(v))) for k, v in itertools.groupby( - sorted(words, key=lambda x: x[0]), key=lambda x: x[0] + sorted(words, key=lambda x: x.lower()[0]), key=lambda x: x.lower()[0] ) ] -def test_exercise3(function_to_test: Callable[[List[str]], List[Tuple[str, int]]]): +def test_exercise3( + function_to_test: Callable[[List[str]], List[Tuple[str, int]]], +): data = get_data_exercise3() assert function_to_test(data) == reference_exercise3(data) @@ -224,7 +227,9 @@ def test_exercise3(function_to_test: Callable[[List[str]], List[Tuple[str, int]] # -def reference_exercise4(my_list: List[Tuple[str, int]]) -> List[Tuple[str, float]]: +def reference_exercise4( + my_list: List[Tuple[str, int]], +) -> List[Tuple[str, float]]: total = sum(map(lambda x: x[1], my_list)) # noqa: C417 return [(letter, freq / total) for letter, freq in my_list] From 884d6d329eac7e40de2d88bfb378058af3b0300e Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Thu, 8 May 2025 12:29:34 +0000 Subject: [PATCH 2/3] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- .../tests/test_11_functional_programming.py | 24 +++++++++---------- 1 file changed, 12 insertions(+), 12 deletions(-) diff --git a/tutorial/tests/test_11_functional_programming.py b/tutorial/tests/test_11_functional_programming.py index 365488f1..d1e785de 100644 --- a/tutorial/tests/test_11_functional_programming.py +++ b/tutorial/tests/test_11_functional_programming.py @@ -90,12 +90,12 @@ def reference_filter_even(my_list: List[int]) -> List[int]: def test_filter_even(function_to_test: Callable, my_list: List[int]): res = function_to_test(my_list) assert isinstance(res, list), "The function you wrote does not return a list" - assert not check_for_loop_in_body( - function_to_test - ), "You are not allowed to use a for loop in this exercise" - assert res == reference_filter_even( - my_list - ), "The list you return is not equal to the expected solution" + assert not check_for_loop_in_body(function_to_test), ( + "You are not allowed to use a for loop in this exercise" + ) + assert res == reference_filter_even(my_list), ( + "The list you return is not equal to the expected solution" + ) # @@ -116,12 +116,12 @@ def reference_add_one(my_list: List[int]) -> List[int]: ], ) def test_add_one(function_to_test: Callable, my_list: List[int]): - assert function_to_test(my_list) == reference_add_one( - my_list - ), "The list you return is not equal to the expected solution" - assert not check_for_loop_in_body( - function_to_test - ), "You are not allowed to use a for loop in this exercise" + assert function_to_test(my_list) == reference_add_one(my_list), ( + "The list you return is not equal to the expected solution" + ) + assert not check_for_loop_in_body(function_to_test), ( + "You are not allowed to use a for loop in this exercise" + ) # From 3127b1785aff584b984c4bed84bbf243cee629d5 Mon Sep 17 00:00:00 2001 From: Edoardo Baldi Date: Thu, 12 Feb 2026 15:43:10 +0100 Subject: [PATCH 3/3] Fix type hints --- tutorial/tests/test_11_functional_programming.py | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/tutorial/tests/test_11_functional_programming.py b/tutorial/tests/test_11_functional_programming.py index cf430dcf..eecce659 100644 --- a/tutorial/tests/test_11_functional_programming.py +++ b/tutorial/tests/test_11_functional_programming.py @@ -142,7 +142,7 @@ def reference_multiples_of_n(my_list: list[int], k: int) -> list[int]: ], ) def test_multiples_of_n( - function_to_test: Callable[[list[int]], int], + function_to_test: Callable[[list[int], int], int], my_list: list[int], k: int, ): @@ -163,7 +163,7 @@ def reference_exercise1(matrix: list[list[int]]) -> list[list[int]]: [(np.eye(3)), (np.random.randint(0, 100, size=(4, 4)))], ) def test_exercise1( - function_to_test: Callable[[List[List[int]]], List[List[int]]], + function_to_test: Callable[[list[list[int]]], list[list[int]]], my_input: NDArray, ): res = function_to_test(my_input.tolist()) @@ -217,7 +217,7 @@ def reference_exercise3(words: list[str]) -> list[tuple[str, int]]: def test_exercise3( - function_to_test: Callable[[List[str]], List[Tuple[str, int]]], + function_to_test: Callable[[list[str]], list[tuple[str, int]]], ): data = get_data_exercise3() assert function_to_test(data) == reference_exercise3(data) @@ -229,8 +229,8 @@ def test_exercise3( def reference_exercise4( - my_list: List[Tuple[str, int]], -) -> List[Tuple[str, float]]: + my_list: list[tuple[str, int]], +) -> list[tuple[str, float]]: total = sum(map(lambda x: x[1], my_list)) # noqa: C417 return [(letter, freq / total) for letter, freq in my_list]