Python Challenge - Nested list weight sum using recursive function & BFS

Published: 02 March 2025
on channel: Let's Cook the Code
28
1

This question has been asked in big MNC's. Its a good and complex example of using recursive function with logical thinking as well
Problem Statement:
"Nested List Sum with Depth Weighting"

You are given a nested list of integers. The list may contain other nested lists to an arbitrary depth. Write a recursive function to calculate the weighted sum of all integers in the list, where the weight is determined by the depth of each integer.

Weight Calculation:
The deeper the integer is nested, the higher its weight.
Weight is calculated as:
Level 1: Weight = 1
Level 2: Weight = 2
Level 3: Weight = 3
...and so on.


Example input and output
nested_list = [1, [4, [6]]]
# Explanation:
# - 1 is at depth 1 → 1 * 1 = 1
# - 4 is at depth 2 → 4 * 2 = 8
# - 6 is at depth 3 → 6 * 3 = 18
# Total Sum = 1 + 8 + 18 = 27
Output: 27


On this page of the site you can watch the video online Python Challenge - Nested list weight sum using recursive function & BFS with a duration of hours minute second in good quality, which was uploaded by the user Let's Cook the Code 02 March 2025, share the link with friends and acquaintances, this video has already been watched 28 times on youtube and it was liked by 1 viewers. Enjoy your viewing!