Say I have a ragged list, like:
[
[1, 2],
[3,
[4, 5]
]
]
And I want to iterate over each item and delete it one-by-one. However, I don't know the structure of the list.
So, I have to iterate over the list, with the following algorithm:
- Look at the first item of the list.
- If it's a list, concatenate it to the start of the rest of the list.
- Else, delete it.
For example, with the above example [[1, 2], [3, [4, 5]]]
:
[1, 2]
is a list. Concatenate it -[1, 2, [3, [4, 5]]]
1
is a number. Delete it -[2, [3, [4, 5]]]
2
is a number. Delete it -[[3, [4, 5]]]
[3, [4, 5]]
is a list. Concatenate it -[3, [4, 5]]
3
is a number. Delete it -[[4, 5]]
[4, 5]
is a list. Concatenate it -[4, 5]
4
is a number. Delete it -[5]
5
is a number. Delete it -[]
There are no more items left, so our work is done.
Your challenge is to compute the sizes of the lists resulting from applying this. For example, with the above, the list starts with length 2. It then goes to lengths 3, 2, 1, 2, 1, 2, 1, 0.
Standard sequence rules apply - that is, you may take a list \$l\$ and generate all terms, or take a list \$l\$ and a number \$n\$ and calculate the nth or first n terms. Your results may or may not contain the length of the initial list at the start, and the 0 at the end, and you may take \$n\$ 0-indexed or 1-indexed.
Since only the shape of \$l\$ matters, you may take it filled with any consistent value instead of arbitrary integers.
\$l\$ may contain empty lists. In this case, it's concatenated as normal and the length decreases by 1.
This is code-golf, shortest wins!
Testcases
These include the leading length but not the trailing 0. Reference implementation
[[1, 2], [3, [4, 5]]] -> [2, 3, 2, 1, 2, 1, 2, 1]
[[6, 3, [1, 3, 4]], 4, [2, 3, 9, [5, 6]]] -> [3, 5, 4, 3, 5, 4, 3, 2, 1, 4, 3, 2, 1, 2, 1]
[3, 4, 5] -> [3, 2, 1]
[[[[[[[[1]]]]]]]] -> [1, 1, 1, 1, 1, 1, 1, 1]
[] -> []
[1, [], 1] -> [3, 2, 1]
[[],[[[]]],[]] -> [3, 2, 2, 2, 1]
[[],[[[]]],[]] -> [3, 2, 2, 2, 1]
\$\endgroup\$