📘
Concatenate Arrays
Problem Statement
Combine multiple NumPy arrays along different axes.
Background
- np.concatenate([arr1, arr2], axis=0): Stack vertically (default)
- np.concatenate([arr1, arr2], axis=1): Stack horizontally
- np.vstack(): Vertical stack
- np.hstack(): Horizontal stack
Your Task
Write a function concat_arrays(arr1, arr2) that returns a dictionary with:
- "vstack": Vertically stacked array as nested list
- "hstack": Horizontally stacked array as nested list
- "vstack_shape": Shape of vstack result as list
- "hstack_shape": Shape of hstack result as list
Output Format
Return a dictionary with exactly these four keys.
Example:
Input:
arr1 = [[1, 2], [3, 4]], arr2 = [[5, 6], [7, 8]]
Output:
{'vstack': [[1, 2], [3, 4], [5, 6], [7, 8]], 'hstack': [[1, 2, 5, 6], [3, 4, 7, 8]], 'vstack_shape': [4, 2], 'hstack_shape': [2, 4]}Reasoning:
vstack: 2 rows + 2 rows = 4 rows. hstack: 2 cols + 2 cols = 4 cols
Constraints:
- Use np.vstack() and np.hstack()
- Both arrays will have compatible shapes
Editor
Python 3.13.1
Test Results
0/0Run code to see test results.