#K10276. Flattening Nested Lists

    ID: 23211 Type: Default 1000ms 256MiB

Flattening Nested Lists

Flattening Nested Lists

You are given a nested list of integers which may contain other lists nested arbitrarily deep. Your task is to flatten this list into a single list of integers by performing a depth-first traversal.

The process can be described by the following formula in LaTeX:

\[ \text{flatten}(L) = \begin{cases} \ \ [\,] & \text{if } L \text{ is empty},\\ \ \ \text{for each element } x \in L:\; \begin{cases} \ \ \ \text{append } x & \text{if } x \text{ is an integer},\\ \ \ \ \text{append } \text{flatten}(x) & \text{if } x \text{ is a list} \ \ \end{cases} \end{cases} \]

Read the input from standard input and output the flattened list to standard output in JSON format.

inputFormat

The input consists of a single line containing a nested list represented in JSON format. For example:

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

outputFormat

Output the flattened list in JSON array format. For instance, given the sample input above, the output should be:

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