From 3323ed3e37a8335082f9694f6dbfda1050368942 Mon Sep 17 00:00:00 2001 From: Elizaveta Sirotina Date: Fri, 20 Dec 2024 14:45:31 +0100 Subject: [PATCH 1/2] Implemented two functions --- numpy_questions.py | 18 ++++++++++++++++-- 1 file changed, 16 insertions(+), 2 deletions(-) diff --git a/numpy_questions.py b/numpy_questions.py index 07a10c1..108fc60 100644 --- a/numpy_questions.py +++ b/numpy_questions.py @@ -40,7 +40,14 @@ def max_index(X): i = 0 j = 0 - # TODO + if not isinstance(X, np.ndarray): + raise ValueError('Work with Numpy array only') + + if not X.ndim == 2: + raise ValueError('Work with 2D arrays only') + + max_index = np.argmax(X) + i, j = np.unravel_index(max_index, X.shape) return i, j @@ -64,4 +71,11 @@ 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. + product = 1.0 + + for n in range(1, n_terms + 1): + term = (4 * n**2) / (4 * n**2 - 1) + product *= term + + pi_approx = 2 * product + return pi_approx From 95bc04c2db7400d789d7b3325f64e7adf8e09eef Mon Sep 17 00:00:00 2001 From: Elizaveta Sirotina Date: Fri, 20 Dec 2024 14:48:34 +0100 Subject: [PATCH 2/2] Removed whitespaces --- numpy_questions.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/numpy_questions.py b/numpy_questions.py index 108fc60..3bbb733 100644 --- a/numpy_questions.py +++ b/numpy_questions.py @@ -72,10 +72,10 @@ 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. product = 1.0 - + for n in range(1, n_terms + 1): term = (4 * n**2) / (4 * n**2 - 1) product *= term - + pi_approx = 2 * product return pi_approx