Skip to content

Commit 8706168

Browse files
committed
UP my solution
1 parent d31f7ef commit 8706168

File tree

1 file changed

+16
-1
lines changed

1 file changed

+16
-1
lines changed

numpy_questions.py

+16-1
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
This will be enforced with `flake8`. You can check that there is no flake8
1616
errors by calling `flake8` at the root of the repo.
1717
"""
18+
1819
import numpy as np
1920

2021

@@ -41,6 +42,11 @@ def max_index(X):
4142
j = 0
4243

4344
# TODO
45+
if not isinstance(X, np.ndarray):
46+
raise ValueError("The input is not numpy array!")
47+
if X.ndim != 2:
48+
raise ValueError("The input is not a 2D array!")
49+
i, j = np.unravel_index(np.argmax(X), X.shape)
4450

4551
return i, j
4652

@@ -64,4 +70,13 @@ 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+
return 2.
75+
76+
product = 1.0
77+
for i in range(1, n_terms + 1):
78+
numerator = 4 * i ** 2
79+
denominator = (4 * i ** 2) - 1
80+
product *= numerator / denominator
81+
82+
return 2 * product

0 commit comments

Comments
 (0)