Skip to content

Commit 3e936d9

Browse files
committed
UP my solution
1 parent d31f7ef commit 3e936d9

File tree

1 file changed

+22
-6
lines changed

1 file changed

+22
-6
lines changed

numpy_questions.py

+22-6
Original file line numberDiff line numberDiff line change
@@ -37,12 +37,22 @@ 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
4240

43-
# TODO
41+
if not isinstance(X, np.ndarray):
42+
raise ValueError("the input is not a numpy array")
43+
if X.ndim != 2:
44+
raise ValueError("the shape is not 2D")
45+
max_value = None
46+
i, j = np.array(X).shape
4447

45-
return i, j
48+
max_value = None
49+
for x in range(i):
50+
for y in range(j):
51+
cell_value = X[x][y]
52+
if max_value is None or cell_value > max_value:
53+
max_value = cell_value
54+
i_max, j_max = x, y
55+
return i_max, j_max
4656

4757

4858
def wallis_product(n_terms):
@@ -63,5 +73,11 @@ def wallis_product(n_terms):
6373
The approximation of order `n_terms` of pi using the Wallis product.
6474
"""
6575
# XXX : The n_terms is an int that corresponds to the number of
66-
# terms in the product. For example 10000.
67-
return 0.
76+
# terms in the product. For example 10000.*
77+
if n_terms == 0:
78+
pi_approx = 2
79+
else:
80+
pi_approx = 2
81+
for n in range(1, n_terms + 1):
82+
pi_approx *= (4 * n ** 2) / (4 * (n ** 2) - 1)
83+
return pi_approx

0 commit comments

Comments
 (0)