0
0
NumPydata~10 mins

Sorting along axes 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 1D numpy array in ascending order.

NumPy
import numpy as np
arr = np.array([3, 1, 4, 2])
sorted_arr = np.sort([1])
print(sorted_arr)
Drag options to blanks, or click blank then click option'
A[3, 1, 4, 2]
Barr
Cnp.array
Dsorted_arr
Attempts:
3 left
💡 Hint
Common Mistakes
Passing the sorted array variable itself instead of the original array.
Passing the numpy module instead of the array.
2fill in blank
medium

Complete the code to sort the 2D numpy array along axis 0 (columns).

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

Fix the error in the code to sort the 2D array along axis 1 (rows).

NumPy
import numpy as np
arr = np.array([[5, 3], [2, 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.
4fill in blank
hard

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

NumPy
words = ['apple', 'bat', 'carrot', 'dog']
lengths = { [1] : [2] for word in words if len(word) > 3 }
print(lengths)
Drag options to blanks, or click blank then click option'
Aword
Blen(word)
Cword.upper()
Dword.lower()
Attempts:
3 left
💡 Hint
Common Mistakes
Using word.upper() or word.lower() as keys instead of the original word.
Using the word itself as value instead of its length.
5fill in blank
hard

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

NumPy
words = ['apple', 'bat', 'carrot', 'dog']
lengths = { [1] : [2] for word in words if len(word) [3] 3 }
print(lengths)
Drag options to blanks, or click blank then click option'
Aword.upper()
Blen(word)
C>
D<
Attempts:
3 left
💡 Hint
Common Mistakes
Using the wrong comparison operator in the condition.
Using the word itself instead of uppercase for keys.