From ca42ebe1024a190dd46af4de3e6732484b3f18b0 Mon Sep 17 00:00:00 2001 From: calls9-amirbraham Date: Tue, 17 Dec 2024 09:59:22 +0100 Subject: [PATCH 1/2] solution to assignement 1 --- numpy_questions.py | 13 ++++++++++--- 1 file changed, 10 insertions(+), 3 deletions(-) diff --git a/numpy_questions.py b/numpy_questions.py index 07a10c1..ec7f5c6 100644 --- a/numpy_questions.py +++ b/numpy_questions.py @@ -39,8 +39,12 @@ def max_index(X): """ i = 0 j = 0 - - # TODO + if not isinstance(X, np.ndarray): + raise ValueError("Input must be a numpy array") + if X.ndim != 2: + raise ValueError("Input array must be 2D") + + i, j = np.unravel_index(np.argmax(X), X.shape) return i, j @@ -64,4 +68,7 @@ def wallis_product(n_terms): """ # XXX : The n_terms is an int that corresponds to the number of # terms in the product. For example 10000. - return 0. + res = 1 + for i in range(1, n_terms + 1): + res *= 4 * i ** 2 / (4 * i ** 2 - 1) + return 2 * res From 02532d0be06b61d62803b2a78291355111f02e79 Mon Sep 17 00:00:00 2001 From: calls9-amirbraham Date: Tue, 17 Dec 2024 10:05:26 +0100 Subject: [PATCH 2/2] code style check --- numpy_questions.py | 2 -- 1 file changed, 2 deletions(-) diff --git a/numpy_questions.py b/numpy_questions.py index ec7f5c6..ebb0149 100644 --- a/numpy_questions.py +++ b/numpy_questions.py @@ -43,9 +43,7 @@ def max_index(X): raise ValueError("Input must be a numpy array") if X.ndim != 2: raise ValueError("Input array must be 2D") - i, j = np.unravel_index(np.argmax(X), X.shape) - return i, j