Skip to content

Commit 7bd6215

Browse files
committed
UP my solution
1 parent d31f7ef commit 7bd6215

File tree

1 file changed

+12
-5
lines changed

1 file changed

+12
-5
lines changed

numpy_questions.py

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -37,10 +37,11 @@ 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
42-
43-
# TODO
40+
if not isinstance(X, np.ndarray):
41+
raise ValueError("Input must be a numpy array")
42+
if X.ndim != 2:
43+
raise ValueError("Input array must be 2D")
44+
i, j = np.unravel_index(np.argmax(X), X.shape)
4445

4546
return i, j
4647

@@ -64,4 +65,10 @@ def wallis_product(n_terms):
6465
"""
6566
# XXX : The n_terms is an int that corresponds to the number of
6667
# terms in the product. For example 10000.
67-
return 0.
68+
if not isinstance(n_terms, int) or n_terms < 0:
69+
raise ValueError("n_terms must be a non negative integer")
70+
71+
approx = 1.
72+
for i in range(1, n_terms + 1):
73+
approx *= (4 * i**2) / (4 * i**2 - 1)
74+
return approx * 2

0 commit comments

Comments
 (0)