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

Pubblicato il: 02 marzo 2025
sul canale di: 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


In questa pagina del sito puoi guardare il video online Python Challenge - Nested list weight sum using recursive function & BFS della durata di ore minuti seconda in buona qualità , che l'utente ha caricato Let's Cook the Code 02 marzo 2025, condividi il link con amici e conoscenti, su youtube questo video è già stato visto 28 volte e gli è piaciuto 1 spettatori. Buona visione!