Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete the code to check if two arrays can be broadcast together using numpy.
NumPy
import numpy as np arr1 = np.array([1, 2, 3]) arr2 = np.array([[1], [2], [3]]) can_broadcast = np.[1](arr1, arr2) print(can_broadcast.shape)
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using np.broadcast_arrays instead of np.broadcast.
Trying to use np.shape to check broadcasting.
✗ Incorrect
The function np.broadcast(arr1, arr2) returns a broadcast object if the arrays can be broadcast together.
2fill in blank
mediumComplete the code to get the broadcasted shape of two arrays using numpy.
NumPy
import numpy as np shape = np.[1]((3, 1), (1, 4)) print(shape)
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using np.broadcast instead of np.broadcast_shapes.
Trying to broadcast actual arrays instead of shapes.
✗ Incorrect
np.broadcast_shapes returns the shape tuple resulting from broadcasting the input shapes.
3fill in blank
hardFix the error in the code to check if arrays of shapes (2,3) and (3,) can be broadcast together.
NumPy
import numpy as np arr1 = np.zeros((2, 3)) arr2 = np.zeros((3,)) can_broadcast = np.broadcast(arr1, [1]) print(can_broadcast.shape)
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Passing the same array twice to np.broadcast.
Passing shape tuples instead of arrays.
✗ Incorrect
To check broadcasting compatibility, both arrays must be passed to np.broadcast. Here arr2 is the second array.
4fill in blank
hardFill both blanks to create a dictionary of word lengths only for words longer than 4 characters.
NumPy
words = ['apple', 'bat', 'carrot', 'dog', 'elephant'] lengths = {word: [1] for word in words if len(word) [2] 4}
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using the word itself as the dictionary value.
Using '<' instead of '>' in the condition.
✗ Incorrect
We want the length of each word (len(word)) and filter words longer than 4 (len(word) > 4).
5fill in blank
hardFill all three blanks to create a dictionary of uppercase words and their lengths for words longer than 3 characters.
NumPy
words = ['sun', 'moon', 'star', 'sky'] result = { [1]: [2] for w in words if len(w) [3] 3 }
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using original words as keys instead of uppercase.
Using '<' instead of '>' in the condition.
✗ Incorrect
The keys are uppercase words (w.upper()), values are lengths (len(w)), and filter words longer than 3 (len(w) > 3).