Posts: 9
Threads: 5
Joined: Jan 2022
kindly guide me how should I proceed to solve this problem:
[[4,5],[7,8,[20]],100]
Posts: 1,838
Threads: 2
Joined: Apr 2017
Jan-23-2022, 04:25 PM
(This post was last modified: Jan-23-2022, 04:25 PM by ndc85430.)
What have you tried? Hint: recursion is useful for problems like this.
Posts: 9
Threads: 5
Joined: Jan 2022
Jan-23-2022, 04:53 PM
(This post was last modified: Jan-23-2022, 04:53 PM by vlearner.)
(Jan-23-2022, 04:25 PM)ndc85430 Wrote: What have you tried? Hint: recursion is useful for problems like this.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 |
def sum_nestedlist( l ):
total = 0
for j in range ( len (l)):
if type (l[j]) = = list :
sum_nestedlist(l[j])
else :
total + = l[j]
return total
print (sum_nestedlist([[ 1 , 2 , 3 ],[ 4 ,[ 5 , 6 ]], 7 ]))
|
I checked this solution but I am not getting how the code works. can anyone explain the logic of code?
Posts: 1,145
Threads: 114
Joined: Sep 2019
Jan-23-2022, 06:28 PM
(This post was last modified: Jan-23-2022, 06:28 PM by menator01.)
I don't know that i would use range on the list.
All though there are other ways but, maybe like this
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
mylist = [ 5 ,[ 5 , 3 , 3 ], 5 , 5 , 5 ]
def sum_nested_list(mylist):
total = 0
for num in mylist:
if type (num) = = list :
total + = sum (num)
else :
total + = num
return total
print (sum_nested_list(mylist))
|
Output: 31
Posts: 1,838
Threads: 2
Joined: Apr 2017
You wrote code you don't understand?
Posts: 582
Threads: 1
Joined: Aug 2019
Haha @ menator01 , you are cheating!  It would work fine if the list would remain limited to two dimensions, but it it is't. Vlearner had a list with a deeper nesting level: [[4,5],[7,8,[20]],100] . When you run your code on this list you get:
Error: Traceback (most recent call last):
File "/home/ibreeden/PycharmProjects/Forum/forum02.py", line 43, in <module>
print(sum_nested_list(mylist))
File "/home/ibreeden/PycharmProjects/Forum/forum02.py", line 33, in sum_nested_list
total = total + sum(num)
TypeError: unsupported operand type(s) for +: 'int' and 'list'
@ vlearner : your code is almost correct, you must change line 13 to
1 |
total + = sum_nested_list(l[j])
|
... or else you lose the result of your recursive call.
(Jan-23-2022, 04:53 PM)vlearner Wrote: I checked this solution but I am not getting how the code works. can anyone explain the logic of code? The algorithm is straigtforward. If you would pass a list with integers to your function, it would return the sum of the numbers. But if one of the elements of the list is again a (nested) list, then this list is handled by the (recursive) call to the same function. In that case the returned sum must be added to the total.
Posts: 7,315
Threads: 123
Joined: Sep 2016
Jan-23-2022, 07:13 PM
(This post was last modified: Jan-24-2022, 12:59 AM by snippsat.)
Python 🦄🥴❓
1 2 3 4 5 6 7 8 9 10 |
def flatten(seq):
for item in seq:
match item:
case list ():
yield from flatten(item)
case _:
yield item
lst = [[ 1 , 2 , 3 ],[ 4 ,[ 5 , 6 ]], 7 ]
print ( sum (flatten(lst)))
|
Output: 28
menator01 likes this post
Posts: 1,145
Threads: 114
Joined: Sep 2019
(Jan-23-2022, 06:57 PM)ibreeden Wrote: Haha @menator01 , you are cheating! It would work fine if the list would remain limited to two dimensions, but it it is't. Vlearner had a list with a deeper nesting level: [[4,5],[7,8,[20]],100] . When you run your code on this list you get:
Error: Traceback (most recent call last):
File "/home/ibreeden/PycharmProjects/Forum/forum02.py", line 43, in <module>
print(sum_nested_list(mylist))
File "/home/ibreeden/PycharmProjects/Forum/forum02.py", line 33, in sum_nested_list
total = total + sum(num)
TypeError: unsupported operand type(s) for +: 'int' and 'list'
@vlearner : your code is almost correct, you must change line 13 to
1 |
total + = sum_nested_list(l[j])
|
... or else you lose the result of your recursive call.
(Jan-23-2022, 04:53 PM)vlearner Wrote: I checked this solution but I am not getting how the code works. can anyone explain the logic of code? The algorithm is straigtforward. If you would pass a list with integers to your function, it would return the sum of the numbers. But if one of the elements of the list is again a (nested) list, then this list is handled by the (recursive) call to the same function. In that case the returned sum must be added to the total.
You are correct.
Posts: 1,145
Threads: 114
Joined: Sep 2019
(Jan-23-2022, 07:15 PM)menator01 Wrote: (Jan-23-2022, 06:57 PM)ibreeden Wrote: Haha @menator01 , you are cheating! It would work fine if the list would remain limited to two dimensions, but it it is't. Vlearner had a list with a deeper nesting level: [[4,5],[7,8,[20]],100] . When you run your code on this list you get:
Error: Traceback (most recent call last):
File "/home/ibreeden/PycharmProjects/Forum/forum02.py", line 43, in <module>
print(sum_nested_list(mylist))
File "/home/ibreeden/PycharmProjects/Forum/forum02.py", line 33, in sum_nested_list
total = total + sum(num)
TypeError: unsupported operand type(s) for +: 'int' and 'list'
@vlearner : your code is almost correct, you must change line 13 to
1 |
total + = sum_nested_list(l[j])
|
... or else you lose the result of your recursive call.
The algorithm is straigtforward. If you would pass a list with integers to your function, it would return the sum of the numbers. But if one of the elements of the list is again a (nested) list, then this list is handled by the (recursive) call to the same function. In that case the returned sum must be added to the total.
You are correct. 
This seems to work
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
mylist = [ 5 ,[ 5 , [ 9 , 3 ], 3 ], 10 , 8 , 5 ,[ 10 , [ 5 , [ 5 , 2 ]], 8 ]]
def sum_nested_list(mylist):
total = 0
for num in mylist:
if type (num) = = list :
total + = sum_nested_list(num)
else :
total + = num
return total
print ( f 'Final total is {sum_nested_list(mylist)}' )
|
Output: Final total is 78
|