Array Slicing
Problem Statement
Extract portions of a NumPy array using slicing syntax.
Background
NumPy slicing: arr[start:stop:step]
- start: Starting index (inclusive, default 0)
- stop: Ending index (exclusive)
- step: Step size (default 1)
Your Task
Write a function slice_array(arr) that returns a dictionary with:
- "first_three": First 3 elements as list
- "last_three": Last 3 elements as list
- "every_other": Every other element as list
- "reversed": Reversed array as list
Output Format
Return a dictionary with exactly these four keys.
Example
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
{'first_three': [0, 1, 2], 'last_three': [7, 8, 9], 'every_other': [0, 2, 4, 6, 8], 'reversed': [9, 8, 7, 6, 5, 4, 3, 2, 1, 0]}arris a 1D array of integers. Write for the element at position , counting from 0; here , so every value is also its own position.first_threekeeps positions , giving[0, 1, 2].last_threekeeps positions , giving[7, 8, 9].every_otherkeeps the even positions with : , giving[0, 2, 4, 6, 8]. It always starts with the first element, so for an odd the last element is kept too (5 elements give positions 0, 2, 4).reversedfills output position with input position : takes and takes , giving[9, 8, 7, 6, 5, 4, 3, 2, 1, 0].- Each value is a plain list of integers, so the printed dictionary shows bare square brackets.
- Notice that when ,
first_threeandlast_threeare the same three elements, because the two windows overlap completely.
Constraints
- Array will have at least 3 elements
- Use slicing syntax arr[start:stop:step]