NumPy ArraysIntro
Array Slicing and Strides
~8 mincode completion
Implement extract_subarray(arr, start, stop, step) that returns .
Examples
Every other element from index 1 to 8
- Input
- extract_subarray([0, 1, 2, 3, 4, 5, 6, 7, 8, 9], 1, 8, 2)
- Output
- [1, 3, 5, 7]
Full array with step 1
- Input
- extract_subarray([10, 20, 30, 40, 50], 0, 5, 1)
- Output
- [10, 20, 30, 40, 50]
Every third element
- Input
- extract_subarray([0, 1, 2, 3, 4, 5, 6, 7, 8], 0, 9, 3)
- Output
- [0, 3, 6]
Hints
Hint 1
Work directly with the arguments arr, start, stop, step and return the result rather than printing it.
Hint 2
Watch for this: used python list slice.
Requirements
arr: 1D NumPy arraystart: slice start index (inclusive)stop: slice stop index (exclusive)step: stride between elementsReturn Sliced array.
Constraints
Allowed library: NumPy only
Time limit: 200 ms, Memory: 64 MB
Where this shows up
~8 min
••••••••••••••••
8 employers weight this skill
3 frontier labs, 2 quant funds, 2 big tech firms, 1 autonomy company. Top match scores 68.
Python
import numpy as np
def extract_subarray(arr: np.ndarray, start: int, stop: int, step: int) -> np.ndarray:
"""
Return arr[start:stop:step].
Args:
arr: 1D NumPy array
start: slice start index (inclusive)
stop: slice stop index (exclusive)
step: stride between elements
Returns:
Sliced array.
"""
# YOUR CODE HERE
pass