0
0
NumPydata~10 mins

np.argsort() for sort indices in NumPy - Interactive Code Practice

Choose your learning style9 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to get the indices that would sort the array.

NumPy
import numpy as np
arr = np.array([3, 1, 2])
sorted_indices = np.[1](arr)
print(sorted_indices)
Drag options to blanks, or click blank then click option'
Asort
Bargsort
Csorted
Dargmax
Attempts:
3 left
💡 Hint
Common Mistakes
Using np.sort() which returns sorted values, not indices.
Using Python's sorted() which does not work on numpy arrays.
Using np.argmax() which returns index of max value only.
2fill in blank
medium

Complete the code to get the indices that would sort the array in descending order.

NumPy
import numpy as np
arr = np.array([5, 2, 9, 1])
sorted_indices = np.argsort(arr)[[1]]
print(sorted_indices)
Drag options to blanks, or click blank then click option'
A:
B[1:]
C[::-2]
D::-1
Attempts:
3 left
💡 Hint
Common Mistakes
Using [:] which keeps the order ascending.
Using [::-2] which skips elements and reverses partially.
Using [1:] which removes the first element.
3fill in blank
hard

Fix the error in the code to correctly get sorted indices of a 2D array along axis 1.

NumPy
import numpy as np
arr = np.array([[3, 1, 2], [9, 7, 8]])
sorted_indices = np.argsort(arr, axis=[1])
print(sorted_indices)
Drag options to blanks, or click blank then click option'
A1
B0
C2
D-1
Attempts:
3 left
💡 Hint
Common Mistakes
Using axis=0 which sorts columns instead of rows.
Using axis=2 which is invalid for 2D arrays.
Using axis=-1 which is same as axis=1 but might confuse beginners.
4fill in blank
hard

Fill both blanks to create a dictionary with words as keys and their sorted index of length as values, only for words longer than 3 letters.

NumPy
words = ['apple', 'bat', 'carrot', 'dog']
lengths = {word: len(word) for word in words if len(word) [1] 3}
sorted_indices = np.argsort([lengths[word] for word in lengths])
result = {word: sorted_indices[[2]] for [2] in range(len(sorted_indices))}
print(result)
Drag options to blanks, or click blank then click option'
A>
B<
C==
D!=
Attempts:
3 left
💡 Hint
Common Mistakes
Using '<' which filters shorter words.
Using '==' which filters words exactly length 3.
Using '!=' which filters words not length 3.
5fill in blank
hard

Fill all three blanks to create a dictionary mapping each word to its sorted index based on length, only for words longer than 3 letters.

NumPy
words = ['pear', 'kiwi', 'banana', 'fig']
lengths = {word: [1] for word in words if [2](word) > 3}
sorted_indices = np.argsort([lengths[word] for word in lengths])
result = {word: sorted_indices[[3]] for [3] in range(len(sorted_indices))}
print(result)
Drag options to blanks, or click blank then click option'
Alen(word)
Bword
Ci
Dlen
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'word' instead of len(word) for length.
Using 'len' without calling it as a function.
Using 'word' as index variable instead of 'i'.