Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete the code to select elements from array 'a' at indices 1 and 3 using np.take().
NumPy
import numpy as np a = np.array([10, 20, 30, 40, 50]) indices = [1, 3] selected = np.[1](a, indices) print(selected)
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using np.put() instead of np.take() for selection.
Trying to use np.select() which is for conditions.
✗ Incorrect
np.take() selects elements from an array at specified indices.
2fill in blank
mediumComplete the code to replace elements at indices 0 and 2 in array 'a' with values 100 and 200 using np.put().
NumPy
import numpy as np a = np.array([1, 2, 3, 4, 5]) indices = [0, 2] values = [100, 200] np.[1](a, indices, values) print(a)
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using np.take() which only selects elements.
Trying to use np.insert() which inserts elements shifting others.
✗ Incorrect
np.put() replaces elements at given indices with new values.
3fill in blank
hardFix the error in the code to correctly select elements at indices 2 and 4 from array 'a' using np.take().
NumPy
import numpy as np a = np.array([5, 10, 15, 20, 25]) indices = (2, 4) selected = np.[1](a, indices) print(selected)
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using np.put() which is for assignment, not selection.
Using non-existent functions like np.get() or np.fetch().
✗ Incorrect
np.take() accepts tuples or lists for indices to select elements.
4fill in blank
hardFill both blanks to create a dictionary comprehension that maps each word to its length only if the length is greater than 3.
NumPy
words = ['apple', 'bat', 'cat', 'dog', 'elephant'] lengths = {word: [1] for word in words if [2]
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using word instead of len(word) for the value.
Using word > 3 which compares string to number.
✗ Incorrect
We map each word to its length (len(word)) only if length is greater than 3 (len(word) > 3).
5fill in blank
hardFill all three blanks to create a dictionary comprehension that maps uppercase words to their values only if the value is positive.
NumPy
data = {'a': 1, 'b': -2, 'c': 3}
result = { [1]: [2] for k, v in data.items() if v [3] 0 } Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using k.lower() instead of k.upper().
Using v < 0 which filters negative values.
✗ Incorrect
We map uppercase keys (k.upper()) to their values (v) only if value is greater than zero (v > 0).