Complete the code to partially sort the array so the smallest 3 elements are in the first 3 positions.
import numpy as np arr = np.array([7, 2, 5, 3, 9, 1]) result = np.partition(arr, [1]) print(result)
The second argument to np.partition is the index where the array is partitioned. Using 3 puts the smallest 3 elements in the first 3 positions.
Complete the code to get the 2 smallest elements using np.partition.
import numpy as np arr = np.array([10, 4, 6, 2, 8]) partitioned = np.partition(arr, [1]) smallest_two = partitioned[:2] print(smallest_two)
Using np.partition(arr, 1) places the smallest 2 elements in the first two positions (indices 0 and 1).
Fix the error in the code to correctly partition the array to get the 4 smallest elements.
import numpy as np arr = np.array([9, 1, 7, 3, 5]) result = np.partition(arr, [1]) print(result[:4])
The partition index should be 3 to place the 4 smallest elements in the first 4 positions (indices 0 to 3).
Fill both blanks to create a dictionary with words as keys and their lengths as values, but only for words longer than 4 characters.
words = ['apple', 'bat', 'carrot', 'dog', 'elephant'] lengths = { [1] : [2] for word in words if len(word) > 4 } print(lengths)
len as a value instead of len(word).word.upper() as key changes the original word.The dictionary comprehension uses the word as key and its length as value for words longer than 4 characters.
Fill all three blanks to create a dictionary with uppercase words as keys and their lengths as values, but only for words shorter than 6 characters.
words = ['apple', 'bat', 'carrot', 'dog', 'elephant'] result = { [1] : [2] for word in words if len(word) [3] 5 } print(result)
The dictionary comprehension uses uppercase words as keys and their lengths as values for words with length less than or equal to 5.