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

Publié le: 02 mars 2025
sur la chaîne: 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


Sur cette page du site, vous pouvez voir la vidéo en ligne Python Challenge - Nested list weight sum using recursive function & BFS durée heure minute seconde en bonne qualité , qui a été Téléchargé par l'utilisateur Let's Cook the Code 02 mars 2025, Partagez le lien avec vos amis et connaissances, sur youtube cette vidéo a déjà été regardée 28 fois et il a aimé 1 téléspectateurs. Bon visionnage!