Skip to content

Commit dbd30e2

Browse files
committed
numpy
1 parent d31f7ef commit dbd30e2

File tree

1 file changed

+19
-4
lines changed

1 file changed

+19
-4
lines changed

numpy_questions.py

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

43-
# TODO
43+
# Validate input is a numpy array
44+
if not isinstance(X, np.ndarray):
45+
raise ValueError("Input must be a numpy array.")
46+
# Validate input shape
47+
if X.ndim != 2:
48+
raise ValueError("Input array must be 2-dimensional.")
49+
50+
# Find the row and columnd index of the maximum
51+
max_value = np.max(X)
52+
i, j = np.where(X == max_value)
4453

4554
return i, j
4655

@@ -62,6 +71,12 @@ def wallis_product(n_terms):
6271
pi : float
6372
The approximation of order `n_terms` of pi using the Wallis product.
6473
"""
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.
74+
pi = 1
75+
76+
if n_terms < 0:
77+
raise ValueError("n_terms must be a positive integer.")
78+
if n_terms > 0:
79+
for i in range(1, n_terms + 1):
80+
pi *= (4 * i ** 2) / (4 * i ** 2 - 1)
81+
82+
return pi*2

0 commit comments

Comments
 (0)