Challenge - 5 Problems
Fancy Slice Indexing Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate2:00remaining
Output of combining fancy and slice indexing on a 2D array
What is the output of this code snippet?
import numpy as np arr = np.arange(16).reshape(4,4) result = arr[[1,3], 1:3]
NumPy
import numpy as np arr = np.arange(16).reshape(4,4) result = arr[[1,3], 1:3] print(result)
Attempts:
2 left
💡 Hint
Remember that fancy indexing selects specific rows, and slice indexing selects columns.
✗ Incorrect
The fancy index [1,3] selects rows 1 and 3. The slice 1:3 selects columns 1 and 2. So the output is a 2x2 array with elements from rows 1 and 3 and columns 1 and 2.
❓ data_output
intermediate2:00remaining
Shape of array after combining fancy and slice indexing
Given the code below, what is the shape of the resulting array?
import numpy as np arr = np.arange(27).reshape(3,3,3) result = arr[[0,2], :, 1:3]
NumPy
import numpy as np arr = np.arange(27).reshape(3,3,3) result = arr[[0,2], :, 1:3] print(result.shape)
Attempts:
2 left
💡 Hint
Fancy indexing on the first axis selects 2 slices, then full slice on second axis, then slice on third axis.
✗ Incorrect
Fancy indexing selects 2 elements along the first axis, full slice selects all 3 elements along the second axis, and slice 1:3 selects 2 elements along the third axis, so shape is (2,3,2).
🔧 Debug
advanced2:00remaining
Identify the error in combining fancy and slice indexing
What error does this code raise?
import numpy as np arr = np.arange(9).reshape(3,3) result = arr[1:3, [0, 2], 1]
NumPy
import numpy as np arr = np.arange(9).reshape(3,3) result = arr[1:3, [0, 2], 1]
Attempts:
2 left
💡 Hint
Check how many indices the array has and how many are used.
✗ Incorrect
The array is 2D but the code uses 3 indices, causing IndexError: too many indices for array.
🚀 Application
advanced2:00remaining
Extract specific rows and columns using combined indexing
You have a 5x5 numpy array. You want to extract rows 0, 2, and 4, but only columns 1 to 3 (inclusive start, exclusive end). Which code snippet correctly does this?
Attempts:
2 left
💡 Hint
Remember fancy indexing selects rows, slice selects columns.
✗ Incorrect
Option C selects rows 0, 2, 4 and columns 1, 2, 3 correctly. Others select wrong axes or ranges.
🧠 Conceptual
expert2:00remaining
Understanding the output shape when combining fancy and slice indexing
Consider a 3D numpy array with shape (4, 5, 6). If you use fancy indexing on the first axis with 2 indices and slice indexing on the second axis with 3:5, and slice indexing on the third axis with 1:4, what will be the shape of the resulting array?
Attempts:
2 left
💡 Hint
Fancy indexing changes the size of the first axis to the number of indices selected.
✗ Incorrect
Fancy indexing selects 2 elements on axis 0, slice 3:5 selects 2 elements on axis 1, slice 1:4 selects 3 elements on axis 2, so shape is (2, 2, 3).