Challenge - 5 Problems
Absolute Value Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate2:00remaining
Output of np.abs() on mixed array
What is the output of this code?
import numpy as np arr = np.array([-3, 0, 4, -7]) result = np.abs(arr) print(result)
NumPy
import numpy as np arr = np.array([-3, 0, 4, -7]) result = np.abs(arr) print(result)
Attempts:
2 left
💡 Hint
np.abs() returns the absolute value of each element in the array.
✗ Incorrect
np.abs() changes each number to its positive value without changing zero.
❓ data_output
intermediate2:00remaining
Absolute values of a 2D numpy array
Given this 2D array, what is the output of np.abs() applied to it?
import numpy as np arr = np.array([[1, -2], [-3, 4]]) result = np.abs(arr) print(result)
NumPy
import numpy as np arr = np.array([[1, -2], [-3, 4]]) result = np.abs(arr) print(result)
Attempts:
2 left
💡 Hint
np.abs() works element-wise on arrays of any shape.
✗ Incorrect
Each element is converted to its positive value, keeping the shape intact.
🧠 Conceptual
advanced2:00remaining
Effect of np.abs() on complex numbers
What does np.abs() return when applied to a numpy array of complex numbers?
import numpy as np arr = np.array([3+4j, 1-1j]) result = np.abs(arr) print(result)
NumPy
import numpy as np arr = np.array([3+4j, 1-1j]) result = np.abs(arr) print(result)
Attempts:
2 left
💡 Hint
np.abs() returns the magnitude (distance from zero) for complex numbers.
✗ Incorrect
For complex numbers, np.abs() returns the magnitude calculated as sqrt(real^2 + imag^2).
🔧 Debug
advanced2:00remaining
Identify the error in np.abs() usage
What error will this code raise?
import numpy as np arr = np.array(['-3', '4', '-5']) result = np.abs(arr) print(result)
NumPy
import numpy as np arr = np.array(['-3', '4', '-5']) result = np.abs(arr) print(result)
Attempts:
2 left
💡 Hint
np.abs() expects numeric types, not strings.
✗ Incorrect
np.abs() cannot process string arrays directly and raises a TypeError.
🚀 Application
expert3:00remaining
Using np.abs() to find max absolute difference
Given two numpy arrays, which option correctly finds the maximum absolute difference between their elements?
import numpy as np a = np.array([1, -3, 5]) b = np.array([4, -1, -6]) max_diff = ??? print(max_diff)
NumPy
import numpy as np a = np.array([1, -3, 5]) b = np.array([4, -1, -6]) max_diff = ??? print(max_diff)
Attempts:
2 left
💡 Hint
Find the absolute difference element-wise, then get the max.
✗ Incorrect
Subtract arrays element-wise, take absolute values, then find the max value.