From 4ee159f131166065db9e6a539c0b5687ccfeb4d9 Mon Sep 17 00:00:00 2001 From: RasulAlakbarli Date: Fri, 20 Dec 2024 16:29:31 +0100 Subject: [PATCH 1/2] UP my solution --- numpy_questions.py | 23 +++++++++++++++-------- 1 file changed, 15 insertions(+), 8 deletions(-) diff --git a/numpy_questions.py b/numpy_questions.py index 07a10c1..68f4624 100644 --- a/numpy_questions.py +++ b/numpy_questions.py @@ -37,11 +37,17 @@ 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 must be a 2D numpy array.") + + if X.size == 0: + raise ValueError("Input array must not be empty.") + + i, j = np.unravel_index(np.argmax(X), X.shape) return i, j @@ -62,6 +68,7 @@ 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. + pi = 1 + for i in range(1, n_terms + 1): + pi *= (4 * i ** 2) / (4 * i ** 2 - 1) + return 2 * pi \ No newline at end of file From 9ea6e3627f8419fbb81ffd4645a36f21df1a96c1 Mon Sep 17 00:00:00 2001 From: RasulAlakbarli Date: Fri, 20 Dec 2024 16:35:52 +0100 Subject: [PATCH 2/2] UP my solution --- numpy_questions.py | 9 ++++----- 1 file changed, 4 insertions(+), 5 deletions(-) diff --git a/numpy_questions.py b/numpy_questions.py index 68f4624..dd61161 100644 --- a/numpy_questions.py +++ b/numpy_questions.py @@ -37,16 +37,15 @@ def max_index(X): If the input is not a numpy array or if the shape is not 2D. """ - if not isinstance(X, np.ndarray): raise ValueError("Input must be a numpy array.") - + if X.ndim != 2: raise ValueError("Input must be a 2D numpy array.") - + if X.size == 0: raise ValueError("Input array must not be empty.") - + i, j = np.unravel_index(np.argmax(X), X.shape) return i, j @@ -71,4 +70,4 @@ def wallis_product(n_terms): pi = 1 for i in range(1, n_terms + 1): pi *= (4 * i ** 2) / (4 * i ** 2 - 1) - return 2 * pi \ No newline at end of file + return 2 * pi