#C1090. Flatten a Nested List

    ID: 40156 Type: Default 1000ms 256MiB

Flatten a Nested List

Flatten a Nested List

Your task is to write a program that flattens a nested list into a single list while preserving the order of appearance. The nested list may contain integers or other nested lists. The problem is defined recursively as follows:

\(\text{flatten}(L) = \begin{cases} [] & \text{if } L \text{ is empty} \\ [x] & \text{if } L \text{ is an integer } x \\ \text{flatten}(x_1) \oplus \text{flatten}(x_2) \oplus \cdots \oplus \text{flatten}(x_n) & \text{if } L = [x_1,x_2,\dots,x_n] \end{cases}\)

Here, \(\oplus\) denotes concatenation of lists. For example, flattening the list [[1,2,[3,4]],5,[6,[7,8]],9] should yield [1,2,3,4,5,6,7,8,9].

inputFormat

The input consists of a single line read from standard input (stdin). This line is a valid Python-like list literal that represents the nested list. The list can contain integers and other lists. For example:

[[1,2,[3,4]],5,[6,[7,8]],9]

outputFormat

The output should be printed to standard output (stdout). It must be the flattened list of integers, with each integer separated by a single space. For example, the output corresponding to the above input would be:

1 2 3 4 5 6 7 8 9
## sample
[[1,2,[3,4]],5,[6,[7,8]],9]
1 2 3 4 5 6 7 8 9