v9.1.1
Menu

Basic Array Indexing

easyFREESubsetting, Slicing, IndexingNumPy IndexingNumpy

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

Input
[10, 20, 30, 40, 50]
Output
{'first': 10, 'last': 50, 'middle': 30}
Reasoning
  • Input: arr is a 1D array of n=5n = 5 integers, [10, 20, 30, 40, 50]. Positions are counted from zero, so they run 0,1,2,3,40, 1, 2, 3, 4.
  • First: the element at position 00 is 10.
  • Last: the final position is n−1=4n - 1 = 4, holding 50.
  • Middle: the index is ⌊n/2⌋=⌊5/2⌋=2\lfloor n / 2 \rfloor = \lfloor 5/2 \rfloor = 2, and position 22 holds 30. For odd nn this is the true centre, with two elements on each side.
  • Output: a dictionary mapping the keys first, last and middle to whole numbers: {'first': 10, 'last': 50, 'middle': 30}.
  • Notice that for even nn there is no single centre. With the two-element array [100,200][100, 200], ⌊2/2⌋=1\lfloor 2/2 \rfloor = 1 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

More from Numpy

solution.py

Test results

Sign in to run your code against the test cases.