0
0
NumPydata~10 mins

np.sort() for sorting arrays 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 sort the array using numpy.

NumPy
import numpy as np
arr = np.array([3, 1, 2])
sorted_arr = np.[1](arr)
print(sorted_arr)
Drag options to blanks, or click blank then click option'
Asorted
Bsort
Corder
Darrange
Attempts:
3 left
💡 Hint
Common Mistakes
Using Python's built-in sorted() which returns a list, not a numpy array.
Trying to use non-existing functions like np.order() or np.arrange().
2fill in blank
medium

Complete the code to sort the 2D array along rows (axis=1).

NumPy
import numpy as np
arr = np.array([[3, 2, 1], [6, 5, 4]])
sorted_arr = np.sort(arr, axis=[1])
print(sorted_arr)
Drag options to blanks, or click blank then click option'
A1
B0
C-1
D2
Attempts:
3 left
💡 Hint
Common Mistakes
Using axis=0 which sorts columns instead of rows.
Using axis=2 which is invalid for 2D arrays.
3fill in blank
hard

Fix the error in the code to sort the array in-place.

NumPy
import numpy as np
arr = np.array([3, 1, 2])
arr.[1]()
print(arr)
Drag options to blanks, or click blank then click option'
Anp.sort
Bsorted
Csort
Dsort_array
Attempts:
3 left
💡 Hint
Common Mistakes
Using sorted() which returns a list, not sorting in-place.
Trying to call np.sort() as a method on the array.
4fill in blank
hard

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

NumPy
words = ['cat', 'elephant', 'dog', 'horse']
lengths = {word: [1] for word in words if [2]
print(lengths)
Drag options to blanks, or click blank then click option'
Alen(word)
Bword
Clen(word) > 3
Dword > 3
Attempts:
3 left
💡 Hint
Common Mistakes
Using the word itself as the value instead of its length.
Checking if the word string is greater than 3 instead of its length.
5fill in blank
hard

Fill all three blanks to create a dictionary with uppercase words as keys, their lengths as values, only for words longer than 3 letters.

NumPy
words = ['cat', 'elephant', 'dog', 'horse']
lengths = [1]: [2] for word in words if [3]
print(lengths)
Drag options to blanks, or click blank then click option'
Aword.upper()
Blen(word)
Clen(word) > 3
Dword.lower()
Attempts:
3 left
💡 Hint
Common Mistakes
Using lowercase words as keys instead of uppercase.
Using the word itself as values instead of length.
Incorrect filter condition.