Let's define the "unwrapped size" function u
of a nested list l
(containing only lists) by the following rules:
- If
l
is empty, thenu(l)
is 1. - If
l
is non-empty,u(l)
is equal to the sum of the unwrapped sizes of every element inl
, plus one.
Your task is to write a program (or function) that takes a list as input and outputs (or returns) the unwrapped size of the list.
Test Cases:
[] -> 1
[[[]],[]] -> 4
[[[]],[[[[]],[]]],[[[]],[[[[]],[[],[[]]]]]]] -> 19
[[[[]]]] -> 4
This is code-golf, so the shortest program (in bytes) wins.
()
instead of[]
? \$\endgroup\$[[[]][]]
instead of this[[[]],[]]
in your second example? \$\endgroup\$["This is some text [with square brackets in] ...[& maybe more than one pair]"]
? \$\endgroup\$]
does seem to be the shortest solution in many languages, there are also a lot of answers that actually solve this challenge via list manipulation, and at least in esolangs counting the occurrences of a fixed character is also quite different from counting the occurrences of an input character. \$\endgroup\$