Skip to content

Commit 4ee159f

Browse files
UP my solution
1 parent d31f7ef commit 4ee159f

File tree

1 file changed

+15
-8
lines changed

1 file changed

+15
-8
lines changed

numpy_questions.py

Lines changed: 15 additions & 8 deletions
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
42-
43-
# TODO
44-
40+
41+
if not isinstance(X, np.ndarray):
42+
raise ValueError("Input must be a numpy array.")
43+
44+
if X.ndim != 2:
45+
raise ValueError("Input must be a 2D numpy array.")
46+
47+
if X.size == 0:
48+
raise ValueError("Input array must not be empty.")
49+
50+
i, j = np.unravel_index(np.argmax(X), X.shape)
4551
return i, j
4652

4753

@@ -62,6 +68,7 @@ def wallis_product(n_terms):
6268
pi : float
6369
The approximation of order `n_terms` of pi using the Wallis product.
6470
"""
65-
# XXX : The n_terms is an int that corresponds to the number of
66-
# terms in the product. For example 10000.
67-
return 0.
71+
pi = 1
72+
for i in range(1, n_terms + 1):
73+
pi *= (4 * i ** 2) / (4 * i ** 2 - 1)
74+
return 2 * pi

0 commit comments

Comments
 (0)