Skip to content

Commit 25d89e2

Browse files
committed
Up my solution
1 parent d31f7ef commit 25d89e2

File tree

1 file changed

+28
-12
lines changed

1 file changed

+28
-12
lines changed

numpy_questions.py

Lines changed: 28 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -39,29 +39,45 @@ def max_index(X):
3939
"""
4040
i = 0
4141
j = 0
42-
43-
# TODO
44-
45-
return i, j
42+
if not isinstance(X, np.ndarray):
43+
raise ValueError("Input must be a numpy array.")
44+
if X.ndim == 2:
45+
max_index = []
46+
max_value = []
47+
for n in range(X.shape[0]):
48+
max_index.append(np.argmax(X[n]))
49+
max_value.append(X[n, max_index[-1]])
50+
max_index2 = np.argmax(max_value)
51+
i = max_index2
52+
j = max_index[max_index2]
53+
print(max_index, max_index2)
54+
return i, j
55+
else:
56+
raise ValueError("input must be a 2-dimension array")
4657

4758

4859
def wallis_product(n_terms):
49-
"""Implement the Wallis product to compute an approximation of pi.
60+
"""Compute an approximation of π using the Wallis product formula.
5061
5162
See:
5263
https://en.wikipedia.org/wiki/Wallis_product
5364
5465
Parameters
5566
----------
5667
n_terms : int
57-
Number of steps in the Wallis product. Note that `n_terms=0` will
58-
consider the product to be `1`.
68+
Number of terms in the Wallis product. Must be >= 1.
5969
6070
Returns
6171
-------
62-
pi : float
63-
The approximation of order `n_terms` of pi using the Wallis product.
72+
pi_approx : float
73+
Approximation of π using the Wallis product with `n_terms`.
6474
"""
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.
75+
if n_terms == 0:
76+
return 2
77+
else:
78+
product = 1.0
79+
for i in range(1, n_terms + 1):
80+
term = (4 * i**2) / (4 * i**2 - 1)
81+
product *= term
82+
83+
return product * 2

0 commit comments

Comments
 (0)