#C1750. Flatten Nested List
Flatten Nested List
Flatten Nested List
You are given a nested list that may contain integers, strings, or other lists. Your task is to "flatten" this list, i.e. convert it into a single-level list containing all the elements in the order they appear.
For example, if the input is [1, [2, [3, 4]], 5]
, then the output should be [1,2,3,4,5]
.
Write a program that reads the input from standard input (stdin) and writes the flattened list as a JSON formatted array to standard output (stdout).
Note: If there are nested lists, all their elements are recursively extracted.
You may find the following recursive relation helpful:
$$\text{flatten}(L) = \begin{cases} [] & \text{if } L \text{ is empty},\\ [x] & \text{if } L \text{ is a single element not a list},\\ \text{flatten}(first\ part) \oplus \text{flatten}(rest\ of\ the\ list) & \text{if } L = [first, rest] \\ \end{cases}$$inputFormat
The input consists of a single line containing the JSON representation of a nested list. The list may contain integers, strings (enclosed in double quotes), and/or other nested lists.
outputFormat
Output the flattened list in JSON format on a single line. The order of the elements must be preserved.
## sample[1, [2, [3, 4]], 5]
[1,2,3,4,5]