Flatten a Nested List
Riverbend Removals packs small boxes inside larger ones, with no limit on the depth — a crate can hold a box that holds another box. The packing note is written as a nested list: a plain number is an item, and anything in square brackets is a box still to be opened. The insurance schedule wants one flat line of item numbers, in the order they would come out if a packer worked left to right, opening every box the moment they reached it.
Task: Print every item number on one line, in the order it would be reached.
Input
A single line holding a Python-style list literal, such as [1, [2, 3], [4, [5, 6]]]. It holds integers and further lists, nested to any depth. The starter code already turns that line into a real list, so there is nothing to parse yourself.
Output
One line holding every integer, separated by single spaces, in the order described above.
Example:
Input:
[1, [2, 3], [4, [5, 6]]]
Output:
1 2 3 4 5 6
Sign in to solve this problem
Reading problems is free for everyone — solving them (Run, Submit, and tracking what you've solved) needs an account.
Sign in