#K94212. Flatten Nested List

    ID: 38592 Type: Default 1000ms 256MiB

Flatten Nested List

Flatten Nested List

You are given a nested list of numbers. A nested list is a list that may contain integers (or floating‐point numbers) as well as other nested lists. Your task is to flatten this structure into a single list containing all the numbers in the order they appear.

More formally, if you are given a nested list \( L \), you should recursively extract every number from \( L \) and output them in sequence. That is, if \( L = [a_1, a_2, \ldots, a_n] \), where each \( a_i \) is either a number or another list, then the flattened list is obtained by concatenating the flattenings of each \( a_i \). In latex notation, the flattening function \( f \) can be defined as:

[ f(L) = \begin{cases} [a] & \text{if } L = a \text{ (a number)} \ f(L_1) \mathbin{\Vert} f(L_2) \mathbin{\Vert} \cdots \mathbin{\Vert} f(L_k) & \text{if } L = [L_1, L_2, \ldots, L_k] \end{cases} ]

For example, if the input is [1, [2, [3, [4]]]], then the output should be 1 2 3 4.

inputFormat

The input consists of a single line read from stdin representing a nested list in Python list notation. The list can contain integers, floating-point numbers, or nested lists. For example:

[1, [2, [3, [4]]]]

outputFormat

Output a single line to stdout which contains the flattened list. The numbers should be printed in order, separated by a single space. If the flattened list is empty, print an empty line.

## sample
[1, [2, [3, [4]]]]
1 2 3 4