FunctionRacerProblemsRace

Nested Sum

BeginnerRecursionPython, JavaScript, TypeScript

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

Race itor take your time

Write it in your browser, against the clock. The model solution, stepped through line by line, comes after.

More Recursion problems