Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 18 additions & 3 deletions numpy_questions.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
"""Assignment - using numpy and making a PR.

The goals of this assignment are:
* Use numpy in practice with two easy exercises.
* Use automated tools to validate the code (`pytest` and `flake8`)
* Submit a Pull-Request on github to practice `git`.

Expand Down Expand Up @@ -40,7 +39,15 @@ def max_index(X):
i = 0
j = 0

# TODO
if not isinstance(X, np.ndarray):
raise ValueError("Input must be a numpy array.")
if X.ndim != 2:
raise ValueError("Input array must be 2D.")

# Find the flattened index of the maximum
idx_flat = np.argmax(X)
# Update i and j using the flattened index and array shape
i, j = np.unravel_index(idx_flat, X.shape)

return i, j

Expand All @@ -64,4 +71,12 @@ 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:
return 2.0

# Calculate the product terms using the formula for pi/2
k = np.arange(1, n_terms + 1)
half_pi = np.prod(4.0 * k**2 / (4.0 * k**2 - 1.0))

return 2.0 * half_pi