PIXELBANKv9.1.0
Menu

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:

  1. Flattens the input array
  2. 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:

Input:
[[1, 2, 3], [4, 5, 6]]
Output:
{'original_shape': [2, 3], 'flattened': [1, 2, 3, 4, 5, 6], 'size': 6}
Reasoning:

2x3 array has 6 elements when flattened

Constraints:

  • Use flatten() or ravel()
  • Input can be any dimensional array
🔒

Editor locked

The code editor is locked for Pro problems. It is only available for free problems. Please upgrade to gain access to the code editor for all problems.

solution.py

Test Results

0/0
Run code to see test results.