Skip to content
BytePatterns

Depth Weighted Nested Sum

EasyRecursion#recursion#pass-down~15m

Problem

A list holds integers and further lists, nested to any depth. Items in the outer list sit at depth 1, items in a list inside it at depth 2, and so on. Return the sum of every integer multiplied by the depth it sits at.

Examples

Input:  items = [[1, 1], 2, [1, 1]]
Output: 10
Why:    four 1s at depth 2 give 8, and the 2 at depth 1 gives 2
Input:  items = [1, [4, [6]]]
Output: 27
Why:    1 * 1 + 4 * 2 + 6 * 3
Input:  items = [[[]]]
Output: 0
Why:    edge case, deep nesting with no integers adds nothing

Hints

0 / 3

Stuck on the idea rather than the code? Return Up or Pass Down covers it.