0
0
NumPydata~20 mins

Why sorting matters in NumPy - Challenge Your Understanding

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Sorting Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2:00remaining
Output of sorting a NumPy array
What is the output of this code that sorts a NumPy array?
NumPy
import numpy as np
arr = np.array([3, 1, 4, 1, 5])
sorted_arr = np.sort(arr)
print(sorted_arr)
A[5 4 3 1 1]
B[3 1 4 1 5]
C[1 1 3 4 5]
D[1 3 4 5]
Attempts:
2 left
💡 Hint
Think about what sorting does to the order of elements.
data_output
intermediate
2:00remaining
Number of elements after sorting with unique
What is the number of unique elements after sorting this NumPy array?
NumPy
import numpy as np
arr = np.array([2, 3, 2, 5, 3, 7])
unique_sorted = np.unique(arr)
print(len(unique_sorted))
A4
B5
C6
D3
Attempts:
2 left
💡 Hint
Unique elements are counted after sorting and removing duplicates.
🔧 Debug
advanced
2:00remaining
Identify the error in sorting a 2D array
What error does this code raise when trying to sort a 2D NumPy array incorrectly?
NumPy
import numpy as np
arr = np.array([[3, 2], [1, 4]])
sorted_arr = np.sort(arr, axis=2)
print(sorted_arr)
AValueError: could not broadcast input array
BIndexError: axis 2 is out of bounds for array of dimension 2
CTypeError: sort() missing required argument 'axis'
DNo error, prints sorted array
Attempts:
2 left
💡 Hint
Check the dimensions of the array and the axis parameter.
🚀 Application
advanced
2:00remaining
Sorting to find median in NumPy
Which code correctly finds the median of a NumPy array by sorting?
Amedian = np.mean(np.sort(arr))
Bmedian = np.median(arr)
Cmedian = np.sort(arr)[len(arr)//2]
D
sorted_arr = np.sort(arr)
median = sorted_arr[len(arr)//2]
Attempts:
2 left
💡 Hint
Median is the middle value after sorting.
🧠 Conceptual
expert
2:00remaining
Why sorting is important in data science
Why is sorting data important before applying many data science algorithms?
ASorting organizes data to enable efficient searching and grouping.
BSorting changes data values to improve model accuracy.
CSorting removes duplicates automatically from data.
DSorting encrypts data to protect privacy.
Attempts:
2 left
💡 Hint
Think about how sorted data helps algorithms work faster or better.