Flatten and Ravel Arrays
Problem Statement
Convert a multi-dimensional array to 1D using flatten() and ravel().
Background
Both flatten() and ravel() convert arrays to 1D:
- flatten(): Always returns a copy
- ravel(): Returns a view when possible (more memory efficient)
Your Task
Write a function flatten_array(arr) that:
- Flattens the input array
- Returns a dictionary with:
- "original_shape": Original shape as list
- "flattened": Flattened array as list
- "size": Total number of elements
Output Format
Return a dictionary with exactly these three keys.
Example
[[1, 2, 3], [4, 5, 6]]
{'original_shape': [2, 3], 'flattened': [1, 2, 3, 4, 5, 6], 'size': 6}- Input: a grid of integers,
[[1, 2, 3], [4, 5, 6]]— 2 rows, each holding 3 values. Inputs may have any number of axes, e.g. a cube. - Shape: record the length along each axis in order, rows first then columns, giving
[2, 3]. One integer per axis. - Element count: the entry count is the product of the dimensions, . For the cube it would be .
- One-dimensional sequence: read the grid row by row, left to right — row 0 gives , then row 1 gives — producing
[1, 2, 3, 4, 5, 6]. Its length always equals the element count, 6. - Output: a dictionary with keys
original_shape,flattenedandsize, holding those three results. - Notice that the shape is the only record of the grid's structure: the same six values could equally have been laid out as a or grid.
Constraints
- Use flatten() or ravel()
- Input can be any dimensional array