diff --git a/numpy_questions.py b/numpy_questions.py index 07a10c1..d59fb63 100644 --- a/numpy_questions.py +++ b/numpy_questions.py @@ -29,7 +29,7 @@ def max_index(X): Returns ------- (i, j) : tuple(int) - The row and columnd index of the maximum. + The row and column index of the maximum. Raises ------ @@ -37,12 +37,15 @@ def max_index(X): If the input is not a numpy array or if the shape is not 2D. """ - i = 0 - j = 0 + if not isinstance(X, np.ndarray): + raise ValueError("The input must be a numpy array.") - # TODO + if X.ndim != 2: + raise ValueError("The input array must be 2D.") - return i, j + max_idx = np.unravel_index(np.argmax(X, axis=None), X.shape) + + return max_idx def wallis_product(n_terms): @@ -64,4 +67,15 @@ def wallis_product(n_terms): """ # XXX : The n_terms is an int that corresponds to the number of # terms in the product. For example 10000. - return 0. + if n_terms == 0: + wallis_product = 1 + else: + wallis_product = 1 + for n in range(n_terms): + term1 = (4*(n+1)**2) + term2 = term1-1 + wallis_product = wallis_product*(term1/term2) + + pi = 2*float(wallis_product) + + return pi