From 49cb0721d3e482b7bf7a6d2ab81cb87e06b4f7d9 Mon Sep 17 00:00:00 2001 From: Oudoum Ali Houmed <135204733+OudoumAlihoumed@users.noreply.github.com> Date: Fri, 19 Dec 2025 10:05:14 +0100 Subject: [PATCH] UP my solution --- numpy_questions.py | 21 ++++++++++++++++++--- 1 file changed, 18 insertions(+), 3 deletions(-) diff --git a/numpy_questions.py b/numpy_questions.py index 07a10c1..6db82b2 100644 --- a/numpy_questions.py +++ b/numpy_questions.py @@ -1,7 +1,6 @@ """Assignment - using numpy and making a PR. The goals of this assignment are: - * Use numpy in practice with two easy exercises. * Use automated tools to validate the code (`pytest` and `flake8`) * Submit a Pull-Request on github to practice `git`. @@ -40,7 +39,15 @@ 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.") + + # Find the flattened index of the maximum + idx_flat = np.argmax(X) + # Update i and j using the flattened index and array shape + i, j = np.unravel_index(idx_flat, X.shape) return i, j @@ -64,4 +71,12 @@ 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: + return 2.0 + + # Calculate the product terms using the formula for pi/2 + k = np.arange(1, n_terms + 1) + half_pi = np.prod(4.0 * k**2 / (4.0 * k**2 - 1.0)) + + return 2.0 * half_pi