From 7779cc8a33371acdbc0a7720c85834d66e1fe089 Mon Sep 17 00:00:00 2001 From: MahammadNuriyev62 Date: Fri, 20 Dec 2024 17:34:00 +0100 Subject: [PATCH] UP my solution --- numpy_questions.py | 24 ++++++++++++++++-------- 1 file changed, 16 insertions(+), 8 deletions(-) diff --git a/numpy_questions.py b/numpy_questions.py index 07a10c1..efe2b33 100644 --- a/numpy_questions.py +++ b/numpy_questions.py @@ -15,21 +15,22 @@ This will be enforced with `flake8`. You can check that there is no flake8 errors by calling `flake8` at the root of the repo. """ + import numpy as np -def max_index(X): +def max_index(x: np.ndarray) -> tuple: """Return the index of the maximum in a numpy array. Parameters ---------- - X : ndarray of shape (n_samples, n_features) + x : ndarray of shape (n_samples, n_features) The input array. Returns ------- (i, j) : tuple(int) - The row and columnd index of the maximum. + The row and column index of the maximum. Raises ------ @@ -37,11 +38,15 @@ def max_index(X): If the input is not a numpy array or if the shape is not 2D. """ - 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") + # index = x.argmax() + # i = index // x.shape[1] + # j = index % x.shape[1] + i, j = np.unravel_index(np.argmax(x), x.shape) return i, j @@ -64,4 +69,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. + pi = 1.0 + for i in range(1, n_terms + 1): + pi *= (4 * i**2) / (4 * i**2 - 1) + return 2 * pi