Challenge - 5 Problems
Array Arithmetic Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate2:00remaining
Element-wise addition of NumPy arrays
What is the output of this code snippet that adds two NumPy arrays element-wise?
Data Analysis Python
import numpy as np arr1 = np.array([1, 2, 3]) arr2 = np.array([4, 5, 6]) result = arr1 + arr2 print(result)
Attempts:
2 left
💡 Hint
Think about how NumPy adds arrays of the same shape.
✗ Incorrect
NumPy adds arrays element-wise when they have the same shape. So each element in arr1 is added to the corresponding element in arr2.
❓ data_output
intermediate2:00remaining
Element-wise multiplication with broadcasting
Given the arrays below, what is the output of the element-wise multiplication?
Data Analysis Python
import numpy as np arr1 = np.array([[1, 2, 3], [4, 5, 6]]) arr2 = np.array([10, 20, 30]) result = arr1 * arr2 print(result)
Attempts:
2 left
💡 Hint
Remember how broadcasting works when multiplying arrays of different shapes.
✗ Incorrect
The 1D array arr2 is broadcast across the rows of arr1, so each element in arr2 multiplies the corresponding column in arr1.
🔧 Debug
advanced2:00remaining
Identify the error in element-wise division
What error does this code raise when performing element-wise division?
Data Analysis Python
import numpy as np arr1 = np.array([1, 2, 0]) arr2 = np.array([0, 2, 3]) result = arr1 / arr2 print(result)
Attempts:
2 left
💡 Hint
Look carefully at division by zero in NumPy arrays.
✗ Incorrect
NumPy raises a RuntimeWarning when dividing by zero but does not stop execution. It returns 'inf' or 'nan' in those positions.
❓ visualization
advanced2:00remaining
Plotting element-wise addition result
Which option correctly plots the element-wise addition of two arrays as a line chart?
Data Analysis Python
import numpy as np import matplotlib.pyplot as plt arr1 = np.array([1, 3, 5, 7]) arr2 = np.array([2, 4, 6, 8]) result = arr1 + arr2 plt.plot(result) plt.show()
Attempts:
2 left
💡 Hint
plt.plot draws a line chart by default connecting the points.
✗ Incorrect
plt.plot draws a line chart connecting the values in the array. The result array is [3, 7, 11, 15].
🧠 Conceptual
expert2:00remaining
Effect of element-wise operations on array shape
If you multiply a (3,1) shaped NumPy array by a (1,4) shaped array element-wise, what is the shape of the result?
Attempts:
2 left
💡 Hint
Think about how broadcasting expands dimensions to match.
✗ Incorrect
Broadcasting expands the (3,1) array across columns and the (1,4) array across rows, resulting in a (3,4) array.