Basic 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 (as int)
- "last": Last element (as int)
- "middle": Middle element (use len//2 for index, as int)
Output Format
Return a dictionary with exactly these three keys.
Example
[10, 20, 30, 40, 50]
{'first': 10, 'last': 50, 'middle': 30}- Input:
arris a one-dimensional array of whole numbers,[10, 20, 30, 40, 50]. Positions are counted from , so they run . - First: the element at position is .
- Last: the final position is , which holds . Counting backwards from the end, it is the 1st element from the right.
- Middle: the middle position is , which holds . With an odd length this is the exact centre: two elements sit on each side of it.
- Output: a dictionary with keys
first,lastandmiddlemapped to plain integers:{'first': 10, 'last': 50, 'middle': 30}. - Notice what happens with an even length such as : , so the middle is , the later of the two central values, and it equals the last element. For a one-element array, all three keys hold the same number.
Constraints
- Use standard indexing arr[i]
- Use negative indexing for last element
- Array will have at least 1 element