Basic Array Indexing
Problem Statement
Access elements from a NumPy array using basic indexing.
Background
NumPy supports powerful indexing:
- arr[i] - single element
- arr[i, j] - element at row i, column j (for 2D)
- Negative indices count from end
Your Task
Write a function get_elements(arr) that returns a dictionary with:
- "first": First element
- "last": Last element
- "middle": Middle element (use len//2 for index)
Output Format
Return a dictionary with exactly these three keys. Each value must be a plain Python int (not a NumPy scalar), so the printed dictionary looks exactly like the sample.
Example
[10, 20, 30, 40, 50]
{'first': 10, 'last': 50, 'middle': 30}- Input:
arris a 1D array of integers,[10, 20, 30, 40, 50]. Positions are counted from zero, so they run . - First: the element at position is
10. - Last: the final position is , holding
50. - Middle: the index is , and position holds
30. For odd this is the true centre, with two elements on each side. - Output: a dictionary mapping the keys
first,lastandmiddleto whole numbers:{'first': 10, 'last': 50, 'middle': 30}. - Notice that for even there is no single centre. With the two-element array , picks the upper of the two middle elements,
200, which is also the last element. For a one-element array, all three values are that same element.
Constraints
- Use standard indexing arr[i]
- Use negative indexing for last element
- Array will have at least 1 element