Challenge - 5 Problems
Power and Square Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate2:00remaining
Output of np.power() with array and scalar
What is the output of this code snippet?
NumPy
import numpy as np arr = np.array([1, 2, 3]) result = np.power(arr, 3) print(result)
Attempts:
2 left
💡 Hint
np.power raises each element to the given power.
✗ Incorrect
np.power(arr, 3) raises each element of arr to the power 3, so 1^3=1, 2^3=8, 3^3=27.
❓ Predict Output
intermediate2:00remaining
Difference between np.square() and np.power()
What is the output of this code?
NumPy
import numpy as np arr = np.array([2, 4, 6]) result = np.square(arr) print(result)
Attempts:
2 left
💡 Hint
np.square is like np.power with exponent 2.
✗ Incorrect
np.square(arr) squares each element: 2^2=4, 4^2=16, 6^2=36.
❓ data_output
advanced2:00remaining
Result shape after np.power with broadcasting
What is the shape of the result array after this operation?
NumPy
import numpy as np arr1 = np.array([[1, 2], [3, 4]]) arr2 = np.array([2, 3]) result = np.power(arr1, arr2) print(result.shape)
Attempts:
2 left
💡 Hint
Broadcasting matches shapes to perform element-wise operations.
✗ Incorrect
arr2 shape (2,) broadcasts to arr1 shape (2,2), so result shape is (2,2).
🔧 Debug
advanced2:00remaining
Identify the error in np.power usage
What error does this code raise?
NumPy
import numpy as np arr = np.array([1, 2, 3]) result = np.power(arr, '2') print(result)
Attempts:
2 left
💡 Hint
The exponent must be a number, not a string.
✗ Incorrect
np.power expects numeric types; passing a string causes TypeError.
🚀 Application
expert3:00remaining
Using np.square to compute Euclidean distances
Given two 2D points, which code correctly computes the squared Euclidean distance using np.square?
Attempts:
2 left
💡 Hint
Squared distance is sum of squared differences of coordinates.
✗ Incorrect
Option A squares element-wise differences then sums them, correctly computing squared distance.