#C14050. Flatten a Deeply Nested List

    ID: 43657 Type: Default 1000ms 256MiB

Flatten a Deeply Nested List

Flatten a Deeply Nested List

You are given a nested list of integers. The nested list can be arbitrarily deep, and your task is to flatten it into a single list of integers while preserving the order.

The input is given as a JSON array, where each element is either an integer or another JSON array. Your output should be a flattened JSON array.

Note: The input will be free of circular references.

More formally, if the input list is denoted by \(L\), then the flattened list \(F(L)\) is defined recursively as follows:

\[ F(L)= \begin{cases} [] & \text{if } L= [] \\ \text{if } L= [x_1, x_2, \ldots, x_n], \text{ then } F(L) = \begin{cases} x_1 & \text{if } x_1 \text{ is an integer}\\ F(x_1) & \text{if } x_1 \text{ is a list} \end{cases} \;\Vert\; F([x_2, \ldots, x_n]) \end{cases} \]

where \(\Vert\) denotes the concatenation of lists.

inputFormat

The input is provided via standard input (stdin) as a single line containing a valid JSON array, which represents a nested list. For example:

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

outputFormat

Output the flattened list as a JSON array to standard output (stdout). For the example above, the output should be:

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