Skip to content

Commit 5e8845a

Browse files
author
Youna Maillie
committed
UP my solution
1 parent d31f7ef commit 5e8845a

File tree

1 file changed

+21
-4
lines changed

1 file changed

+21
-4
lines changed

numpy_questions.py

+21-4
Original file line numberDiff line numberDiff line change
@@ -37,11 +37,17 @@ def max_index(X):
3737
If the input is not a numpy array or
3838
if the shape is not 2D.
3939
"""
40-
i = 0
41-
j = 0
40+
# Test if the input is a numpy array or not
41+
if not isinstance(X, np.ndarray):
42+
raise ValueError("Input is not a numpy array.")
4243

43-
# TODO
44+
# Test if the input is in 2D
45+
if X.ndim != 2:
46+
raise ValueError("Input array is not in 2D.")
4447

48+
# Return the index tupple corresponding
49+
# to the maximum in the input numpy array
50+
i, j = np.unravel_index(np.argmax(X), X.shape)
4551
return i, j
4652

4753

@@ -64,4 +70,15 @@ def wallis_product(n_terms):
6470
"""
6571
# XXX : The n_terms is an int that corresponds to the number of
6672
# terms in the product. For example 10000.
67-
return 0.
73+
product = 1.0
74+
if n_terms == 0:
75+
return product * 2
76+
77+
for n in range(1, n_terms + 1):
78+
numerator = 4 * n**2
79+
denominator = numerator - 1
80+
product *= numerator / denominator
81+
82+
pi_approx = 2*product
83+
84+
return pi_approx

0 commit comments

Comments
 (0)