From 6d6542a5b15c35e18942f7346dcb8c6fe9123365 Mon Sep 17 00:00:00 2001 From: Karim Date: Mon, 16 Dec 2024 20:00:31 +0100 Subject: [PATCH 1/4] UP my solution --- numpy_questions.py | 21 +++++++++++---------- 1 file changed, 11 insertions(+), 10 deletions(-) diff --git a/numpy_questions.py b/numpy_questions.py index 07a10c1..21ffb2d 100644 --- a/numpy_questions.py +++ b/numpy_questions.py @@ -1,5 +1,4 @@ """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`) @@ -20,17 +19,14 @@ def max_index(X): """Return the index of the maximum in a numpy array. - Parameters ---------- X : ndarray of shape (n_samples, n_features) The input array. - Returns ------- (i, j) : tuple(int) The row and columnd index of the maximum. - Raises ------ ValueError @@ -39,24 +35,25 @@ def max_index(X): """ i = 0 j = 0 - # TODO - + # error handling + if not isinstance(X, np.ndarray): + raise ValueError("The input is not a numpy array") + if len(X.shape) != 2: + raise ValueError("The shape is not 2D") + i, j = np.unravel_index(np.argmax(X), X.shape) return i, j def wallis_product(n_terms): """Implement the Wallis product to compute an approximation of pi. - See: https://en.wikipedia.org/wiki/Wallis_product - Parameters ---------- n_terms : int Number of steps in the Wallis product. Note that `n_terms=0` will consider the product to be `1`. - Returns ------- pi : float @@ -64,4 +61,8 @@ 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 = 2.0 + for i in range(1, n_terms + 1): + product *= (4 * i ** 2) / (4 * i ** 2 - 1) + return product From d0b22f620cc13e3a862f93ffab014161db95ae34 Mon Sep 17 00:00:00 2001 From: Karim Date: Wed, 18 Dec 2024 09:44:26 +0100 Subject: [PATCH 2/4] UP my solution --- numpy_questions.py | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/numpy_questions.py b/numpy_questions.py index 21ffb2d..e88b05b 100644 --- a/numpy_questions.py +++ b/numpy_questions.py @@ -14,10 +14,12 @@ 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): + """Return the index of the maximum in a numpy array. Parameters ---------- @@ -33,6 +35,7 @@ def max_index(X): If the input is not a numpy array or if the shape is not 2D. """ + i = 0 j = 0 # TODO @@ -46,6 +49,7 @@ def max_index(X): def wallis_product(n_terms): + """Implement the Wallis product to compute an approximation of pi. See: https://en.wikipedia.org/wiki/Wallis_product @@ -59,6 +63,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. From d2d344ce2f6eec83841cbda7b0edd6b0971a3809 Mon Sep 17 00:00:00 2001 From: Karim Date: Wed, 18 Dec 2024 09:52:35 +0100 Subject: [PATCH 3/4] UP my solution --- numpy_questions.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/numpy_questions.py b/numpy_questions.py index e88b05b..5520784 100644 --- a/numpy_questions.py +++ b/numpy_questions.py @@ -1,4 +1,5 @@ """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`) @@ -14,13 +15,13 @@ 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): """Return the index of the maximum in a numpy array. + Parameters ---------- X : ndarray of shape (n_samples, n_features) @@ -35,7 +36,6 @@ def max_index(X): If the input is not a numpy array or if the shape is not 2D. """ - i = 0 j = 0 # TODO @@ -51,6 +51,7 @@ def max_index(X): def wallis_product(n_terms): """Implement the Wallis product to compute an approximation of pi. + See: https://en.wikipedia.org/wiki/Wallis_product Parameters @@ -63,7 +64,6 @@ 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. From 38fdba043273b4ec12e15a3332dc8a0f442fd41a Mon Sep 17 00:00:00 2001 From: Karim Date: Wed, 18 Dec 2024 09:53:57 +0100 Subject: [PATCH 4/4] UP my solution --- numpy_questions.py | 2 -- 1 file changed, 2 deletions(-) diff --git a/numpy_questions.py b/numpy_questions.py index 5520784..ebb656a 100644 --- a/numpy_questions.py +++ b/numpy_questions.py @@ -19,7 +19,6 @@ def max_index(X): - """Return the index of the maximum in a numpy array. Parameters @@ -49,7 +48,6 @@ def max_index(X): def wallis_product(n_terms): - """Implement the Wallis product to compute an approximation of pi. See: