Skip to content

Commit 46c73e5

Browse files
committed
Numpy solve
1 parent d31f7ef commit 46c73e5

File tree

1 file changed

+16
-4
lines changed

1 file changed

+16
-4
lines changed

numpy_questions.py

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,14 @@ def max_index(X):
4040
i = 0
4141
j = 0
4242

43-
# TODO
43+
if not isinstance(X, np.ndarray):
44+
raise ValueError("Input must be a numpy array.")
45+
46+
if X.ndim != 2:
47+
raise ValueError("Input array must be 2-dimensional.")
48+
49+
max_idx = np.unravel_index(np.argmax(X), X.shape)
50+
i, j = max_idx
4451

4552
return i, j
4653

@@ -62,6 +69,11 @@ def wallis_product(n_terms):
6269
pi : float
6370
The approximation of order `n_terms` of pi using the Wallis product.
6471
"""
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.
72+
product = 1.0
73+
74+
for n in range(1, n_terms + 1):
75+
product *= (4 * n ** 2) / ((4 * n ** 2) - 1)
76+
77+
pi = product * 2
78+
79+
return pi

0 commit comments

Comments
 (0)