From 59b893f439abf58c3c79d259f4eef0c34dd55e8d Mon Sep 17 00:00:00 2001 From: Thibault Poux Date: Fri, 20 Dec 2024 17:22:07 +0100 Subject: [PATCH 1/2] Working properly --- numpy_questions.py | 17 +++++++++++++++-- 1 file changed, 15 insertions(+), 2 deletions(-) diff --git a/numpy_questions.py b/numpy_questions.py index 07a10c1..b6f4711 100644 --- a/numpy_questions.py +++ b/numpy_questions.py @@ -40,7 +40,14 @@ def max_index(X): i = 0 j = 0 - # TODO + if not isinstance(X, np.ndarray) : + raise ValueError + if len(X.shape) != 2 : + raise ValueError + + index_linked = np.argmax(X).ravel() + + i, j = np.unravel_index(index_linked, X.shape) return i, j @@ -64,4 +71,10 @@ 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. + if (n_terms==0) : + pi = 1 + else : + n = np.arange(1, n_terms +1, dtype=np.float64) # Generate n = 1, 2, ..., n_terms + ratios = (4 * n**2) / ((2 * n - 1) * (2 * n + 1)) + pi = np.prod(ratios) + return pi*2 From 74181999f5f5d144e53673c78f239202860557a6 Mon Sep 17 00:00:00 2001 From: Thibault Poux Date: Fri, 20 Dec 2024 17:32:12 +0100 Subject: [PATCH 2/2] format code properly --- numpy_questions.py | 15 ++++++++------- 1 file changed, 8 insertions(+), 7 deletions(-) diff --git a/numpy_questions.py b/numpy_questions.py index b6f4711..942cec0 100644 --- a/numpy_questions.py +++ b/numpy_questions.py @@ -40,13 +40,13 @@ def max_index(X): i = 0 j = 0 - if not isinstance(X, np.ndarray) : + if not isinstance(X, np.ndarray): raise ValueError - if len(X.shape) != 2 : + if len(X.shape) != 2: raise ValueError - + index_linked = np.argmax(X).ravel() - + i, j = np.unravel_index(index_linked, X.shape) return i, j @@ -71,10 +71,11 @@ 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. - if (n_terms==0) : + if (n_terms == 0): pi = 1 - else : - n = np.arange(1, n_terms +1, dtype=np.float64) # Generate n = 1, 2, ..., n_terms + else: + # Generate n = 1, 2, ..., n_terms + n = np.arange(1, n_terms + 1, dtype=np.float64) ratios = (4 * n**2) / ((2 * n - 1) * (2 * n + 1)) pi = np.prod(ratios) return pi*2