A museum stores its recovered artifacts inside a nested system of time capsules. Every capsule either directly holds a single artifact, tagged with an integer id, or holds a bundle of smaller sub-capsules, each of which follows this very same rule. Given the complete nesting structure of one shipment, determine the order in which artifact tags would be read out if every capsule in the shipment were opened and unpacked, left to right, depth first.
A single line containing one string that describes the shipment as a bracketed literal:
[ element , element , ... ] (it may be empty: []).element is either an integer (optionally prefixed with -), representing a single artifact tag, or a nested capsule written using this same bracketed literal grammar ([...]), which itself contains zero or more further elements.Print the artifact tags encountered during a left-to-right, depth-first unpacking of the shipment, separated by single spaces, in the order they appear once every capsule is fully opened. If the shipment holds no artifacts at all, print an empty line.
Example 1
Input
[1,[2,[3,4],5],6]
Expected
1 2 3 4 5 6
Explanation
Reading the shipment left to right: the first element is artifact 1. The second element is a nested capsule containing artifact 2, then a further nested capsule holding artifacts 3 and 4, then artifact 5. The third element is artifact 6. Unpacking everything depth-first yields 1, 2, 3, 4, 5, 6.
Example 2
Input
[-3,[],[7,[-8,9]]]
Expected
-3 7 -8 9
Explanation
The first element is artifact -3. The second element is an empty capsule, contributing nothing. The third element is a nested capsule holding artifact 7, followed by a further nested capsule holding artifacts -8 and 9. Unpacking depth-first yields -3, 7, -8, 9.
Ready to solve this?
Sign in to open the editor, run your code against the sample tests, and submit against the full test suite.
Sign in to solve →