NumPy Arrays
Beginner
Array Slicing and Strides
Beginner
~8 min
code completion
Array Slicing and Strides
NumPy arrays support rich slicing with the syntax arr[start:stop:step].
start: index to begin (inclusive)stop: index to end (exclusive)step: how many elements to skipExamples:
arr = np.arange(10) # [0 1 2 3 4 5 6 7 8 9] arr[1:8:2] # [1 3 5 7] -- every 2nd element from index 1 arr[::-1] # [9 8 7 ... 0] -- reversed
Your task:
Implement extract_subarray(arr, start, stop, step) that returns arr[start:stop:step].
Example Tests
Every other element from index 1 to 8
Input: {"arr":[0,1,2,3,4,5,6,7,8,9],"step":2,"stop":8,"start":1}
Expected: [1,3,5,7]
Full array with step 1
Input: {"arr":[10,20,30,40,50],"step":1,"stop":5,"start":0}
Expected: [10,20,30,40,50]
Every third element
Input: {"arr":[0,1,2,3,4,5,6,7,8],"step":3,"stop":9,"start":0}
Expected: [0,3,6]