0
0
NumPydata~20 mins

np.sort() for sorting arrays in NumPy - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Sorting Master with np.sort()
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2:00remaining
Output of np.sort() on a 1D array
What is the output of this code?
import numpy as np
arr = np.array([3, 1, 4, 1, 5])
sorted_arr = np.sort(arr)
print(sorted_arr)
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[1 1 3 4 5]
C[3 1 4 1 5]
D[1 3 4 5]
Attempts:
2 left
💡 Hint
np.sort() returns a sorted copy of the array in ascending order.
data_output
intermediate
2:00remaining
Sorting a 2D array along axis 0
Given this 2D array, what is the output of np.sort(arr, axis=0)?
import numpy as np
arr = np.array([[3, 2], [1, 4]])
sorted_arr = np.sort(arr, axis=0)
print(sorted_arr)
NumPy
import numpy as np
arr = np.array([[3, 2], [1, 4]])
sorted_arr = np.sort(arr, axis=0)
print(sorted_arr)
A
[[3 4]
 [1 2]]
B
[[3 2]
 [1 4]]
C
[[1 2]
 [3 4]]
D
[[1 4]
 [3 2]]
Attempts:
2 left
💡 Hint
Sorting along axis 0 sorts each column independently.
🔧 Debug
advanced
2:00remaining
Identify the error in using np.sort()
What error does this code raise?
import numpy as np
arr = np.array([3, 1, 4])
sorted_arr = np.sort(arr, axis=1)
print(sorted_arr)
NumPy
import numpy as np
arr = np.array([3, 1, 4])
sorted_arr = np.sort(arr, axis=1)
print(sorted_arr)
AAxisError: axis 1 is out of bounds for array of dimension 1
BTypeError: unsupported operand type(s) for +: 'int' and 'str'
CValueError: could not broadcast input array from shape (3,) into shape (1,)
DNo error, prints [1 3 4]
Attempts:
2 left
💡 Hint
Check the dimension of the array and the axis parameter.
🚀 Application
advanced
2:30remaining
Using np.sort() to sort structured arrays
Given this structured array, what is the output of np.sort(arr, order='age')?
import numpy as np
arr = np.array([(1, 'Alice', 25), (2, 'Bob', 20), (3, 'Carol', 30)],
               dtype=[('id', 'i4'), ('name', 'U10'), ('age', 'i4')])
sorted_arr = np.sort(arr, order='age')
print(sorted_arr['name'])
NumPy
import numpy as np
arr = np.array([(1, 'Alice', 25), (2, 'Bob', 20), (3, 'Carol', 30)],
               dtype=[('id', 'i4'), ('name', 'U10'), ('age', 'i4')])
sorted_arr = np.sort(arr, order='age')
print(sorted_arr['name'])
A['Bob' 'Alice' 'Carol']
B['Alice' 'Bob' 'Carol']
C['Carol' 'Alice' 'Bob']
D['Bob' 'Carol' 'Alice']
Attempts:
2 left
💡 Hint
Sorting by 'age' orders the records by the age field ascending.
🧠 Conceptual
expert
1:30remaining
Effect of np.sort() on original array
Which statement about np.sort() is true?
Anp.sort() only works on 1D arrays.
Bnp.sort() sorts the array in place and returns None.
Cnp.sort() modifies the original array and returns the sorted array.
Dnp.sort() returns a sorted copy and does not change the original array.
Attempts:
2 left
💡 Hint
Think about whether np.sort() changes the original array or not.