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

Veröffentlicht am: 02 März 2025
auf dem Kanal: 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


Auf dieser Seite können Sie das Online-Video Python Challenge - Nested list weight sum using recursive function & BFS mit der Dauer stunde minuten sekunde in guter Qualität ansehen, das der Benutzer Let's Cook the Code 02 März 2025 hochgeladen hat, den Link mit Freunden und Bekannten teilen, dieses Video wurde auf Youtube bereits 28 Mal angesehen und es wurde von 1 den Zuschauern gefallen. Viel Spaß beim Betrachtenden Zuschauern gefallen!