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
23 changes: 15 additions & 8 deletions numpy_questions.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,11 +37,17 @@ def max_index(X):
If the input is not a numpy array or
if the shape is not 2D.
"""
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 must be 2D")
max_val = X[0, 0]
Copy link

Copilot AI Dec 17, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This implementation assumes the array has at least one element by accessing X[0, 0]. If an empty array or an array with shape (0, n) or (n, 0) is passed, this will raise an IndexError. The function should handle empty arrays gracefully, either by checking if the array has elements first or by catching this edge case.

Copilot uses AI. Check for mistakes.
i, j = 0, 0
for row in range(X.shape[0]):
for col in range(X.shape[1]):
if X[row, col] > max_val:
max_val = X[row, col]
i, j = row, col
Comment on lines +46 to +50
Copy link

Copilot AI Dec 17, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The nested loop implementation is inefficient for finding the maximum index in a numpy array. NumPy provides built-in optimized functions like np.argmax() or np.unravel_index() that are significantly faster and more idiomatic. Using these would avoid the O(n*m) Python loop overhead and leverage NumPy's vectorized operations.

Copilot uses AI. Check for mistakes.
return i, j


Expand All @@ -62,6 +68,7 @@ def wallis_product(n_terms):
pi : float
The approximation of order `n_terms` of pi using the Wallis product.
"""
# XXX : The n_terms is an int that corresponds to the number of
# terms in the product. For example 10000.
return 0.
product = 1.0
for n in range(1, n_terms + 1):
product *= (4 * n ** 2) / (4 * n ** 2 - 1)
return 2 * product