Design Gurus Logo
Nested List Weight Sum (medium)

Problem Statement

You are given a nestedList. Each element of the nestedList contains either integers or other such nested lists.

The depth level is determined by how many levels of nested lists the integer is within. For example, each element of the list [1, 1, [2], [2, [3]]] represents the depth of itself.

Return the sum of all integers of the list after multiplying each integer with its depth level.

Examples

  • Example 1:

    • Input: [[2, [3, 4]], [5, 6], 7]
    • Expected Output: 54
    • Justification: The sum is calculated as 4 (2 at depth 2) + 9 (3 at depth 3) + 12 (4 at depth 3) + 10 (5 at depth 2) + 12 (6 at depth 2) + 7 (7 at depth 1).
  • Example 2:

    • Input: [1, [4, [6]]]
    • Expected Output: 27
    • Justification: The sum is 1 (depth 1) + 8 (4 at depth 2) + 18 (6 at depth 3).
  • Example 3:

    • Input: [5, [2, [3, [4]]], 1]
    • Expected Output: 35
    • Justification: The calculation is 5 (depth 1) + 4 (2 at depth 2) + 9 (3 at depth 3) + 16 (4 at depth 4) + 1 (depth 1).

Try it yourself

Try solving this question here:

Python3
Python3

. . . .

.....

.....

.....

Unlock this and all other premium problems.
No code editor for this lesson
This lesson focuses on concepts and theory