Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete the code to clip the values in the array to a minimum of 0.
NumPy
import numpy as np arr = np.array([-5, 0, 5, 10]) clipped = np.clip(arr, [1], None) print(clipped)
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using None as minimum value causes error.
Using a positive number greater than 0 clips too many values.
✗ Incorrect
np.clip() limits values below the minimum to the minimum value. Here, minimum is 0.
2fill in blank
mediumComplete the code to clip the values in the array between 2 and 8.
NumPy
import numpy as np arr = np.array([1, 3, 5, 9]) clipped = np.clip(arr, [1], 8) print(clipped)
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Setting minimum to 0 clips too many values.
Setting minimum higher than 2 clips values incorrectly.
✗ Incorrect
Setting minimum to 2 and maximum to 8 clips values below 2 to 2 and above 8 to 8.
3fill in blank
hardFix the error in the code to clip values between 1 and 4.
NumPy
import numpy as np arr = np.array([0, 2, 5, 7]) clipped = np.clip(arr, 1, [1]) print(clipped)
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using a maximum bound larger than 4.
Using a minimum bound larger than maximum.
✗ Incorrect
The maximum bound should be 4 to clip values above 4 down to 4.
4fill in blank
hardFill both blanks to clip values below 10 to 10 and above 20 to 20.
NumPy
import numpy as np arr = np.array([5, 10, 15, 25]) clipped = np.clip(arr, [1], [2]) print(clipped)
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Swapping minimum and maximum values.
Using values outside the specified range.
✗ Incorrect
Minimum is 10 and maximum is 20 to clip values outside this range.
5fill in blank
hardFill all three blanks to create a dictionary with words as keys and their lengths clipped to max 4.
NumPy
words = ['apple', 'bat', 'cat', 'dolphin'] lengths = {word: len(word) if len(word) < [1] else [2] for word in words if len(word) > [3] print(lengths)
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using inconsistent max length values.
Filtering words incorrectly.
✗ Incorrect
Clip lengths to max 4: if length less than 4, keep it; else set to 4. Include words longer than 3.