#K81772. Flattening a Nested List

    ID: 35828 Type: Default 1000ms 256MiB

Flattening a Nested List

Flattening a Nested List

You are given a nested list that may contain integers, strings, or other lists. Your task is to flatten this nested list into a single list of elements in left-to-right order. The structure can be arbitrarily nested, and the output should list the flattened elements separated by a single space.

More formally, if the nested list is denoted by \(L\), you need to implement a function that recursively traverses \(L\) and produces a flat sequence \(F\). For example:

[ \texttt{flatten([1, [2, 3], [4, [5, [6, 7]]], 8]) = [1,2,3,4,5,6,7,8]} ]

Please note that the input is read from standard input (stdin) and the output should be written to standard output (stdout). The input will be a single line representing the nested list using Python-like literal format.

inputFormat

The input consists of one single line containing a literal representation of a nested list. The list elements can be numbers or strings. The nested structure is defined using square brackets [] and elements are separated by commas. For example:

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

outputFormat

The output should be a single line containing the flattened list elements separated by a single space. If the flattened list is empty, output nothing.

For example, for the input:

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

the output should be:

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