📘
2D Array Slicing
EasyNumPy Slicing
Problem Statement
Extract portions of a 2D NumPy array using slicing.
Background
For 2D arrays:
- arr[i, j] - element at row i, column j
- arr[i, :] or arr[i] - entire row i
- arr[:, j] - entire column j
- arr[r1:r2, c1:c2] - submatrix
Your Task
Write a function slice_2d(arr) that returns a dictionary with:
- "top_left": Top-left 2×2 submatrix (as list)
- "bottom_row": Last row (as list)
- "right_col": Rightmost column (as list)
- "center": Center element (as int, for odd dimensions)
Example:
Input:
3×3 array [[1,2,3],[4,5,6],[7,8,9]]
Output:
{'top_left': [[1, 2], [4, 5]], 'bottom_row': [7, 8, 9], 'right_col': [3, 6, 9], 'center': 5}Reasoning:
[:2,:2] for top-left, [-1,:] for bottom, [:,-1] for right, center at (1,1)
Constraints:
- Array will be at least 3×3
- For center, use shape//2 for both dimensions
Editor
Python 3.13.1
Test Results
0/0Run code to see test results.