12
1
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.
2Can input be taken as a string, i.e. with enclosing quote marks? Can we use
()
instead of[]
? – Luis Mendo – 2016-11-10T15:49:07.680can we take input in this format
[[[]][]]
instead of this[[[]],[]]
in your second example? – Mukul Kumar – 2016-11-10T16:50:46.013What is the size of
["This is some text [with square brackets in] ...[& maybe more than one pair]"]
? – Jonathan Allan – 2016-11-10T16:54:22.470@JonathanAllan I'm pretty sure that the input won't contain any non-list types (at least that's what the definition and the test cases suggest and what pretty much every answer so far assumes). – Martin Ender – 2016-11-10T17:01:45.637
@JonathanAllan
u
is not defined for values which aren't a list. – Martin Ender – 2016-11-10T17:19:01.9275
Possible duplicate of Find the occurrences of a character in an input string
– James – 2016-11-11T04:40:48.7932@DrMcMoylex I disagree. While counting the number of
]
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. – Martin Ender – 2016-11-11T08:19:30.910Some answers to this challenge seem to take for granted that input can be given as a string (e.g.
"[ ]"
) and subsequently counts the number of right or left brackets. Are those answers really valid given the specification of the challenge? Given that your language supports some type of list, shouldn't input be a "list-object" rather than just a string? – Billywob – 2016-11-11T14:25:12.183