From 47dd3cdbe2e7e3c2863a9257b06912436585c8df Mon Sep 17 00:00:00 2001 From: Paul Thuret Date: Fri, 20 Dec 2024 13:47:00 +0100 Subject: [PATCH] UP my solution --- numpy_questions.py | 15 +++++++++------ 1 file changed, 9 insertions(+), 6 deletions(-) diff --git a/numpy_questions.py b/numpy_questions.py index 07a10c1..a9485c4 100644 --- a/numpy_questions.py +++ b/numpy_questions.py @@ -37,10 +37,11 @@ def max_index(X): If the input is not a numpy array or if the shape is not 2D. """ - i = 0 - j = 0 + if not isinstance(X, np.ndarray) or X.ndim != 2: + raise ValueError("Input must be a 2D numpy array.") - # TODO + max_index_flat = np.argmax(X) + i, j = np.unravel_index(max_index_flat, X.shape) return i, j @@ -62,6 +63,8 @@ def wallis_product(n_terms): 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. + product = 1.0 + for i in range(1, n_terms + 1): + term = (4 * i**2) / (4 * i**2 - 1) + product *= term + return 2 * product