#K77407. Flatten Nested List
Flatten Nested List
Flatten Nested List
Your task is to implement a function that flattens a nested list of arbitrary depth. The function should traverse the entire structure, extract all integer elements in their original order, and ignore any null
values. In addition, if the function encounters a string element, it should attempt to interpret the string as a list. If the string correctly parses as a list then that list must be flattened, but if not then the string is considered a non‐list entry.
If any non‐list string elements are found (i.e. those that could not be interpreted as a list), the function should not return the flattened list. Instead, it should return a message in one of the following formats:
- If there is exactly one non‐list element:
\(\texttt{There is 1 non-list entry: []}\) - If there are multiple non‐list elements:
\(\texttt{There are N non-list entries: [, , ...]}\)
If no such non‐list entries are encountered, then output the flattened list in the Python list format.
Note: Input is provided as a single line from stdin
in JSON array format. Output your result to stdout
.
inputFormat
A single line containing a JSON array representing the nested list. The list may include integers, nested lists, strings (which may represent a list), and null
values. For example,
[1, [2, "[3, 4]"], [5, [6, 7]], 8]
outputFormat
If the nested list contains any non‐list string elements (i.e. strings that cannot be parsed into a list), output a message exactly as specified. Otherwise, output the completely flattened list in standard Python list format.
Examples:
[1, 2, 3, 4, 5, 6, 7]
There is 1 non-list entry: ['a']## sample
[1, [2, 3], [4, [5, 6]], 7]
[1, 2, 3, 4, 5, 6, 7]