Nested Sum
items is a list whose elements are integers or further lists of the same kind, nested to any depth. Return the sum of every integer found anywhere inside items. Empty lists, at any level, add nothing, so an empty items gives 0.
For example, [1, [2, 3], [[4]], 5] gives 1 + 2 + 3 + 4 + 5 = 15.
Example 1
items [1,[2,3],[[4]],5]
returns 15
Every integer counts no matter how deeply it sits: 1 + 2 + 3 + 4 + 5 = 15.
Example 2
items [[],[[]],7]
returns 7
The empty lists add nothing, so only the 7 counts.
Constraints
itemsholds at most 100 integers and at most 50 lists in total- Lists are nested at most 8 levels deep
-100 <= value <= 100for every integer
Write it in your browser, against the clock. The model solution, stepped through line by line, comes after.
More Recursion problems
- Rising DigitsBeginner
- Ruler PatternBeginner
- Same Both WaysBeginner
- Suffix ChainBeginner
- Wheels in the RowBeginner
- Deep ReverseEasy
- Depth-Weighted SumEasy
- Digit Product StepsEasy