Challenge - 5 Problems
Sign Detection Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate2:00remaining
Output of np.sign() on mixed values
What is the output of the following code?
import numpy as np arr = np.array([-3, 0, 4, -1, 2]) signs = np.sign(arr) print(signs)
NumPy
import numpy as np arr = np.array([-3, 0, 4, -1, 2]) signs = np.sign(arr) print(signs)
Attempts:
2 left
💡 Hint
np.sign returns -1 for negative, 0 for zero, and 1 for positive values.
✗ Incorrect
np.sign returns an array where each element is -1 if the original element is negative, 0 if zero, and 1 if positive. So for [-3, 0, 4, -1, 2], the output is [-1, 0, 1, -1, 1].
❓ data_output
intermediate2:00remaining
Count of positive, negative, and zero values using np.sign()
Given the array below, what is the count of positive, negative, and zero values using np.sign()?
import numpy as np
arr = np.array([5, -2, 0, 7, -8, 0, 3])
signs = np.sign(arr)
counts = {"positive": np.sum(signs == 1), "negative": np.sum(signs == -1), "zero": np.sum(signs == 0)}
print(counts)NumPy
import numpy as np arr = np.array([5, -2, 0, 7, -8, 0, 3]) signs = np.sign(arr) counts = {"positive": np.sum(signs == 1), "negative": np.sum(signs == -1), "zero": np.sum(signs == 0)} print(counts)
Attempts:
2 left
💡 Hint
Count how many elements are 1, -1, and 0 in the signs array.
✗ Incorrect
The array has 3 positive numbers (5,7,3), 2 negative numbers (-2,-8), and 2 zeros (0,0). np.sign returns 1, -1, and 0 respectively, so counts match accordingly.
🔧 Debug
advanced2:00remaining
Identify the error in np.sign usage
What error will this code raise?
import numpy as np arr = np.array([1, -1, 0]) signs = np.sign(arr, out=[0,0,0]) print(signs)
NumPy
import numpy as np arr = np.array([1, -1, 0]) signs = np.sign(arr, out=[0,0,0]) print(signs)
Attempts:
2 left
💡 Hint
The 'out' parameter expects a numpy array, not a list.
✗ Incorrect
The 'out' parameter in np.sign must be a numpy ndarray to store the output. Passing a Python list causes a TypeError.
🚀 Application
advanced2:00remaining
Using np.sign to flip signs conditionally
You want to flip the sign of all negative numbers in an array but keep positive and zero values unchanged. Which code snippet achieves this using np.sign() and numpy operations?
Attempts:
2 left
💡 Hint
Use np.sign to detect negatives and multiply by -1 to flip them.
✗ Incorrect
Option D flips negative values by multiplying them by -1 and keeps others unchanged. Option D converts all to positive, which is not the same as flipping only negatives. Options B and D do not flip negatives correctly.
🧠 Conceptual
expert2:00remaining
Behavior of np.sign with floating point edge cases
What is the output of np.sign() when applied to the array containing special floating point values?
import numpy as np arr = np.array([-0.0, 0.0, np.nan, np.inf, -np.inf]) signs = np.sign(arr) print(signs)
NumPy
import numpy as np arr = np.array([-0.0, 0.0, np.nan, np.inf, -np.inf]) signs = np.sign(arr) print(signs)
Attempts:
2 left
💡 Hint
np.sign returns 0 for both +0.0 and -0.0, and nan for np.nan.
✗ Incorrect
np.sign returns 0 for both +0.0 and -0.0 (no negative zero in output), nan for np.nan, 1 for positive infinity, and -1 for negative infinity.