From 35e230940bb6f89f7329c8a65eba0cad68e491ed Mon Sep 17 00:00:00 2001 From: Zakaria-Adallah Date: Wed, 18 Dec 2024 09:51:57 +0100 Subject: [PATCH 1/3] UP my solution --- numpy_questions.py | 21 ++++++++++++++++----- 1 file changed, 16 insertions(+), 5 deletions(-) diff --git a/numpy_questions.py b/numpy_questions.py index 07a10c1..9756209 100644 --- a/numpy_questions.py +++ b/numpy_questions.py @@ -40,8 +40,14 @@ def max_index(X): i = 0 j = 0 - # TODO + # Validate input + 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") + # Find maximum index + i, j = np.unravel_index(np.argmax(X), X.shape) return i, j @@ -59,9 +65,14 @@ def wallis_product(n_terms): Returns ------- - pi : float + The approximation of order `n_terms` of pi using the Wallis product. """ - # 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: + return 2.0 + + n = np.arange(1, n_terms + 1, dtype=float) + factors = (4 * n * n) / (4 * n * n - 1) + approx_pi = 2.0 * np.prod(factors) + + return approx_pi From 04040d8c32ceb69865540ddb9ab9a6cd0878f489 Mon Sep 17 00:00:00 2001 From: Zakaria-Adallah Date: Wed, 18 Dec 2024 09:54:29 +0100 Subject: [PATCH 2/3] UP my solution --- numpy_questions.py | 1 - 1 file changed, 1 deletion(-) diff --git a/numpy_questions.py b/numpy_questions.py index 9756209..71d0cc8 100644 --- a/numpy_questions.py +++ b/numpy_questions.py @@ -40,7 +40,6 @@ def max_index(X): i = 0 j = 0 - # Validate input if not isinstance(X, np.ndarray): raise ValueError("Input must be a numpy array") if X.ndim != 2: From 600649e356398b15a14ae5529524ca29ec0c0c61 Mon Sep 17 00:00:00 2001 From: Zakaria-Adallah Date: Wed, 18 Dec 2024 10:05:50 +0100 Subject: [PATCH 3/3] UP my solution --- numpy_questions.py | 6 ------ 1 file changed, 6 deletions(-) diff --git a/numpy_questions.py b/numpy_questions.py index 71d0cc8..81e700b 100644 --- a/numpy_questions.py +++ b/numpy_questions.py @@ -45,7 +45,6 @@ def max_index(X): if X.ndim != 2: raise ValueError("Input array must be 2D") - # Find maximum index i, j = np.unravel_index(np.argmax(X), X.shape) return i, j @@ -61,11 +60,6 @@ def wallis_product(n_terms): n_terms : int Number of steps in the Wallis product. Note that `n_terms=0` will consider the product to be `1`. - - Returns - ------- - - The approximation of order `n_terms` of pi using the Wallis product. """ if n_terms == 0: return 2.0