Skip to content

Commit ffa5697

Browse files
committed
UP my solution
1 parent d31f7ef commit ffa5697

File tree

1 file changed

+16
-2
lines changed

1 file changed

+16
-2
lines changed

numpy_questions.py

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,9 +39,15 @@ def max_index(X):
3939
"""
4040
i = 0
4141
j = 0
42+
if not isinstance(X, np.ndarray):
43+
raise ValueError("Input must be a numpy array.")
4244

43-
# TODO
45+
if X.ndim != 2:
46+
raise ValueError("Input array must be 2D.")
4447

48+
# Find the index of the maximum value
49+
max_idx = np.unravel_index(np.argmax(X), X.shape)
50+
i, j = max_idx
4551
return i, j
4652

4753

@@ -64,4 +70,12 @@ 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+
if n_terms < 0:
74+
raise ValueError("Number of terms must be non-negative.")
75+
76+
product = 1.0
77+
for n in range(1, n_terms + 1):
78+
term = (4 * n**2) / ((4 * n**2) - 1)
79+
product *= term
80+
81+
return 2 * product

0 commit comments

Comments
 (0)