From f9657b540d969637ac1bc5b63ff3af5a12114701 Mon Sep 17 00:00:00 2001 From: MellissaHAFIS Date: Mon, 16 Dec 2024 14:40:00 +0100 Subject: [PATCH 01/11] Up my solution --- numpy_questions.py | 2 ++ 1 file changed, 2 insertions(+) diff --git a/numpy_questions.py b/numpy_questions.py index 07a10c1..0c0c21d 100644 --- a/numpy_questions.py +++ b/numpy_questions.py @@ -65,3 +65,5 @@ 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. + +print("hello") \ No newline at end of file From 0ed5c01e48dc57ec589721111efed2c57eb8f69e Mon Sep 17 00:00:00 2001 From: MellissaHAFIS Date: Mon, 16 Dec 2024 15:27:43 +0100 Subject: [PATCH 02/11] Up my solution --- numpy_questions.py | 34 ++++++++++++++++++++++++++++++---- 1 file changed, 30 insertions(+), 4 deletions(-) diff --git a/numpy_questions.py b/numpy_questions.py index 0c0c21d..9d58b64 100644 --- a/numpy_questions.py +++ b/numpy_questions.py @@ -41,7 +41,12 @@ def max_index(X): j = 0 # TODO - + rowMax=np.max(X, axis=1) + maxValue=np.max(rowMax) + indices= np.where(X==maxValue) + i=int(indices[0]) + j=int(indices[1]) + #return indices return i, j @@ -64,6 +69,27 @@ 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. - -print("hello") \ No newline at end of file + if n_terms==0: + wallis_product=1 + else: + wallis_product=1 + for n in range(n_terms): + wallis_product=wallis_product*((4*(n+1)**2) / (4*(n+1)**2 -1)) + + pi=2*wallis_product + + return pi + +#trying the first function +X=np.array( + [ + [12,4, 1], + [1, 78, 20] + ] +) + +print(max_index(X)) + +#trying the second function + +print("The approximation if the number pi of the order 100000 is: ", wallis_product(100000)) \ No newline at end of file From aabbdada91868bd5d3eb1b1fa47b3c339b57880e Mon Sep 17 00:00:00 2001 From: MellissaHAFIS Date: Mon, 16 Dec 2024 15:30:28 +0100 Subject: [PATCH 03/11] Up my solution --- numpy_questions.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/numpy_questions.py b/numpy_questions.py index 9d58b64..5903474 100644 --- a/numpy_questions.py +++ b/numpy_questions.py @@ -92,4 +92,4 @@ def wallis_product(n_terms): #trying the second function -print("The approximation if the number pi of the order 100000 is: ", wallis_product(100000)) \ No newline at end of file +print("The approximation if the number pi of the order 1000 is: ", wallis_product(1000)) \ No newline at end of file From 28345d2b09c96e9f44667cce249301dd9ce01925 Mon Sep 17 00:00:00 2001 From: MellissaHAFIS Date: Mon, 16 Dec 2024 15:37:31 +0100 Subject: [PATCH 04/11] up my solution --- numpy_questions.py | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/numpy_questions.py b/numpy_questions.py index 5903474..e1c31f1 100644 --- a/numpy_questions.py +++ b/numpy_questions.py @@ -74,9 +74,11 @@ def wallis_product(n_terms): else: wallis_product=1 for n in range(n_terms): - wallis_product=wallis_product*((4*(n+1)**2) / (4*(n+1)**2 -1)) + term1=(4*(n+1)**2) + term2=term1-1 + wallis_product=wallis_product*(term1/term2) - pi=2*wallis_product + pi=2*float(wallis_product) return pi From e9cbe26e8e6af5c7645e2cf279f397ac10eb94bd Mon Sep 17 00:00:00 2001 From: MellissaHAFIS Date: Mon, 16 Dec 2024 16:06:18 +0100 Subject: [PATCH 05/11] Up my solution --- numpy_questions.py | 13 +++++-------- 1 file changed, 5 insertions(+), 8 deletions(-) diff --git a/numpy_questions.py b/numpy_questions.py index e1c31f1..687071c 100644 --- a/numpy_questions.py +++ b/numpy_questions.py @@ -41,12 +41,15 @@ def max_index(X): j = 0 # TODO + max_idx = np.argmax(X) + i, j = np.unravel_index(max_idx, X.shape) + ''' rowMax=np.max(X, axis=1) maxValue=np.max(rowMax) indices= np.where(X==maxValue) i=int(indices[0]) j=int(indices[1]) - #return indices + ''' return i, j @@ -88,10 +91,4 @@ def wallis_product(n_terms): [12,4, 1], [1, 78, 20] ] -) - -print(max_index(X)) - -#trying the second function - -print("The approximation if the number pi of the order 1000 is: ", wallis_product(1000)) \ No newline at end of file +) \ No newline at end of file From 18707e0d0b60838a742fb63967ec413844c9af6e Mon Sep 17 00:00:00 2001 From: MellissaHAFIS Date: Mon, 16 Dec 2024 16:14:33 +0100 Subject: [PATCH 06/11] up my solution --- numpy_questions.py | 26 +++++++++----------------- 1 file changed, 9 insertions(+), 17 deletions(-) diff --git a/numpy_questions.py b/numpy_questions.py index 687071c..b06fb52 100644 --- a/numpy_questions.py +++ b/numpy_questions.py @@ -45,8 +45,8 @@ def max_index(X): i, j = np.unravel_index(max_idx, X.shape) ''' rowMax=np.max(X, axis=1) - maxValue=np.max(rowMax) - indices= np.where(X==maxValue) + maxValue=np.max(rowMax) + indices= np.where(X==maxValue) i=int(indices[0]) j=int(indices[1]) ''' @@ -72,23 +72,15 @@ 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. - if n_terms==0: + if n_terms == 0: + wallis_product = 1 + else: wallis_product=1 - else: - wallis_product=1 - for n in range(n_terms): - term1=(4*(n+1)**2) + for n in range(n_terms): + term1=(4*(n+1)**2) term2=term1-1 wallis_product=wallis_product*(term1/term2) - - pi=2*float(wallis_product) - return pi + pi = 2*float(wallis_product) -#trying the first function -X=np.array( - [ - [12,4, 1], - [1, 78, 20] - ] -) \ No newline at end of file + return pi From 85fc23e93a57cbdb6f6a6006220623c9813d3119 Mon Sep 17 00:00:00 2001 From: MellissaHAFIS Date: Mon, 16 Dec 2024 16:18:24 +0100 Subject: [PATCH 07/11] up my solution --- numpy_questions.py | 24 +++++++++++------------- 1 file changed, 11 insertions(+), 13 deletions(-) diff --git a/numpy_questions.py b/numpy_questions.py index b06fb52..59d8868 100644 --- a/numpy_questions.py +++ b/numpy_questions.py @@ -41,15 +41,13 @@ def max_index(X): j = 0 # TODO - max_idx = np.argmax(X) - i, j = np.unravel_index(max_idx, X.shape) - ''' - rowMax=np.max(X, axis=1) - maxValue=np.max(rowMax) - indices= np.where(X==maxValue) - i=int(indices[0]) - j=int(indices[1]) - ''' + + rowMax = np.max(X, axis=1) + maxValue = np.max(rowMax) + indices = np.where(X == maxValue) + i = int(indices[0]) + j = int(indices[1]) + return i, j @@ -75,11 +73,11 @@ def wallis_product(n_terms): if n_terms == 0: wallis_product = 1 else: - wallis_product=1 + wallis_product = 1 for n in range(n_terms): - term1=(4*(n+1)**2) - term2=term1-1 - wallis_product=wallis_product*(term1/term2) + term1 = (4*(n+1)**2) + term2 = term1-1 + wallis_product = wallis_product*(term1/term2) pi = 2*float(wallis_product) From 05b7e6e6ca008e816c3a6153de84e7b0bc3d294f Mon Sep 17 00:00:00 2001 From: MellissaHAFIS Date: Mon, 16 Dec 2024 16:22:36 +0100 Subject: [PATCH 08/11] Up my solution --- numpy_questions.py | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/numpy_questions.py b/numpy_questions.py index 59d8868..581ebdb 100644 --- a/numpy_questions.py +++ b/numpy_questions.py @@ -45,8 +45,8 @@ def max_index(X): rowMax = np.max(X, axis=1) maxValue = np.max(rowMax) indices = np.where(X == maxValue) - i = int(indices[0]) - j = int(indices[1]) + i = indices[0][0] + j = indices[1][0] return i, j @@ -82,3 +82,5 @@ def wallis_product(n_terms): pi = 2*float(wallis_product) return pi + +print(max_index([[0, 1], [2, 0]])) \ No newline at end of file From 0ac2b0884473b3c536d4f91ff6ae0b074cb4ab01 Mon Sep 17 00:00:00 2001 From: MellissaHAFIS Date: Mon, 16 Dec 2024 16:24:01 +0100 Subject: [PATCH 09/11] up my solution --- numpy_questions.py | 2 -- 1 file changed, 2 deletions(-) diff --git a/numpy_questions.py b/numpy_questions.py index 581ebdb..b08e7fb 100644 --- a/numpy_questions.py +++ b/numpy_questions.py @@ -82,5 +82,3 @@ def wallis_product(n_terms): pi = 2*float(wallis_product) return pi - -print(max_index([[0, 1], [2, 0]])) \ No newline at end of file From e344828be7b2c32c61c3243e37b8193fe52b3beb Mon Sep 17 00:00:00 2001 From: MellissaHAFIS Date: Mon, 16 Dec 2024 16:36:28 +0100 Subject: [PATCH 10/11] up my solution --- numpy_questions.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/numpy_questions.py b/numpy_questions.py index b08e7fb..66b5154 100644 --- a/numpy_questions.py +++ b/numpy_questions.py @@ -41,7 +41,7 @@ def max_index(X): j = 0 # TODO - + X = np.array(X) rowMax = np.max(X, axis=1) maxValue = np.max(rowMax) indices = np.where(X == maxValue) From aca550a38e64db082a378bc02696f7cc1d67c75c Mon Sep 17 00:00:00 2001 From: MellissaHAFIS Date: Tue, 17 Dec 2024 06:06:10 +0100 Subject: [PATCH 11/11] up my solution --- numpy_questions.py | 23 ++++++++++------------- 1 file changed, 10 insertions(+), 13 deletions(-) diff --git a/numpy_questions.py b/numpy_questions.py index 66b5154..d59fb63 100644 --- a/numpy_questions.py +++ b/numpy_questions.py @@ -29,7 +29,7 @@ def max_index(X): Returns ------- (i, j) : tuple(int) - The row and columnd index of the maximum. + The row and column index of the maximum. Raises ------ @@ -37,18 +37,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 - X = np.array(X) - rowMax = np.max(X, axis=1) - maxValue = np.max(rowMax) - indices = np.where(X == maxValue) - i = indices[0][0] - j = indices[1][0] - - return i, j + if not isinstance(X, np.ndarray): + raise ValueError("The input must be a numpy array.") + + if X.ndim != 2: + raise ValueError("The input array must be 2D.") + + max_idx = np.unravel_index(np.argmax(X, axis=None), X.shape) + + return max_idx def wallis_product(n_terms):