@@ -112,8 +112,8 @@ def product_sum_iterative(arr: list[int | list]) -> int:
112112 eg. queue -> [(arr, depth, multiplication_factor)]
113113
114114 2. Loop until queue is empty
115- 1. Pop starting element of queue
116- 2. Iterate on starting element of queue
115+ 1. Take front item from Queue and pop it
116+ 2. Iterate on front element
117117 If current element is nested list
118118 - then add that into queue with updated depth
119119 and multiplication factor
@@ -128,33 +128,37 @@ def product_sum_iterative(arr: list[int | list]) -> int:
128128
129129 Step 0
130130 Queue - [([5, 2, [-7, 1], 3, [6, [-13, 8], 4]], 1, 1)]
131- top element - [5, 2, [-7, 1], 3, [6, [-13, 8], 4]]
132- depth - 1
133- multiplication factor - 1
131+ Queue front item -
132+ List - [5, 2, [-7, 1], 3, [6, [-13, 8], 4]]
133+ depth - 1
134+ multiplication factor - 1
134135
135136 product sum = 0 (previous) + 5 * 1 + 2 * 1 + 3 * 1 = 10
136137 -------------------------------------------------------
137138 Step 1
138139 Queue - [([-7, 1], 2, 2), ([6, [-13, 8], 4], 2, 2)]
139- top element - [-7, 1]
140- depth - 2
141- multiplication factor - 2
140+ Queue front item -
141+ List - [-7, 1]
142+ depth - 2
143+ multiplication factor - 2
142144
143145 product sum = 10 (previous) + (-7) * 2 + 1 * 2 = -2
144146 -------------------------------------------------------
145147 Step 2
146148 Queue - [([6, [-13, 8], 4], 2, 2)]
147- top element - [6, [-13, 8], 4]
148- depth - 2
149- multiplication factor - 2
149+ Queue front item -
150+ List - [6, [-13, 8], 4]
151+ depth - 2
152+ multiplication factor - 2
150153
151154 product sum = -2 (previous) + 6 * 2 + 4 * 2 = 18
152155 -------------------------------------------------------
153156 Step 3
154157 Queue - [([-13, 8], 3, 6)]
155- top element - [-13, 8]
156- depth - 3
157- multiplication factor - 6
158+ Queue front item -
159+ List - [-13, 8]
160+ depth - 3
161+ multiplication factor - 6
158162
159163 product sum = 18 (previous) + (-13) * 6 + 8 * 6 = -12
160164 -------------------------------------------------------
@@ -182,9 +186,9 @@ def product_sum_iterative(arr: list[int | list]) -> int:
182186 product_sum = 0
183187
184188 while queue :
185- starting_element , depth , multiplication_factor = queue .pop (0 )
189+ queue_front_list , depth , multiplication_factor = queue .pop (0 )
186190
187- for element in starting_element :
191+ for element in queue_front_list :
188192 if isinstance (element , list ):
189193 queue .append ((element , depth + 1 , multiplication_factor * (depth + 1 )))
190194 else :
0 commit comments