#C2289. Flatten and Sort a Nested List
Flatten and Sort a Nested List
Flatten and Sort a Nested List
Given a nested list that may contain integers as well as other nested lists, your task is to flatten the list and then sort the resulting flat list in ascending order.
More formally, if \(L\) is a nested list, you need to compute:
\(output = sorted(flatten(L))\)
where flatten
extracts all integers present in L
regardless of nesting.
For example, if the input is [3, [4, [5, [6], 1], 2]]
, the output should be [1, 2, 3, 4, 5, 6]
.
inputFormat
The input is provided via standard input (stdin) as a single line containing a nested list in a format similar to Python lists. The list can include integers and nested sub-lists.
Examples of valid input:
[3, [4, [5, [6], 1], 2]] [[9, 4, [1]], 7, [[3], 6]]
outputFormat
The output should be printed to standard output (stdout) as a single line. It must display the flattened and sorted list in the same Python list format.
Example:
[1, 2, 3, 4, 5, 6]## sample
[3, [4, [5, [6], 1], 2]]
[1, 2, 3, 4, 5, 6]