0
0
NumPydata~10 mins

Why sorting matters in NumPy - Test Your Understanding

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

Complete the code to sort the array in ascending order.

NumPy
import numpy as np
arr = np.array([3, 1, 4, 1, 5])
sorted_arr = np.[1](arr)
print(sorted_arr)
Drag options to blanks, or click blank then click option'
Asort
Bsorted
Corder
Darrange
Attempts:
3 left
💡 Hint
Common Mistakes
Using np.sort which sorts in place and returns None
Using a non-existent function like np.order
2fill in blank
medium

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

NumPy
import numpy as np
arr = np.array([10, 2, 8, 6])
indices = np.[1](arr)
print(indices)
Drag options to blanks, or click blank then click option'
Asort
Bargsort
Csorted
Dorder
Attempts:
3 left
💡 Hint
Common Mistakes
Using np.sort which returns sorted values, not indices
Using np.sorted which is not a NumPy function
3fill in blank
hard

Fix the error in the code to sort the array in descending order.

NumPy
import numpy as np
arr = np.array([7, 3, 9, 1])
sorted_arr = np.sort(arr)[1][::-1]
print(sorted_arr)
Drag options to blanks, or click blank then click option'
A+
B*
C(no operator)
D-
Attempts:
3 left
💡 Hint
Common Mistakes
Adding an operator like + or * before the slice causes syntax error
Trying to use np.sort(arr)[::-1] but adding extra characters
4fill in blank
hard

Fill both blanks to create a dictionary of word lengths for words longer than 3 letters.

NumPy
words = ['data', 'is', 'fun', 'and', 'powerful']
lengths = {word: [1] for word in words if len(word) [2] 3}
print(lengths)
Drag options to blanks, or click blank then click option'
Alen(word)
B>
C<
Dword
Attempts:
3 left
💡 Hint
Common Mistakes
Using the wrong comparison operator like '<'
Using the word itself as the value instead of its length
5fill in blank
hard

Fill all three blanks to create a dictionary with uppercase keys and values for words longer than 2 letters.

NumPy
words = ['cat', 'is', 'big', 'and', 'red']
result = [1]: [2] for w in words if len(w) [3] 2
print(result)
Drag options to blanks, or click blank then click option'
Aw.upper()
Bw
C>
Dlen(w)
Attempts:
3 left
💡 Hint
Common Mistakes
Using length as value instead of the word
Using wrong comparison operator like '<='