Sum the List of Lists
Your task is to create a function that takes a deeply nested list of integers and calculates the sum of all the integers in the list. The list can be of any depth and the depth can vary between different branches of the list.
The input of your program will be a single line that contains a string representation of a deeply nested list. Each list is denoted by square brackets
[]
, and its elements are separated by commas ,
. Each nested list is contained within another list. All elements are positive integers. You can load the lists with eval(input())
.The output of your program should be a single integer - the sum of all integers contained in the nested list.
Input | Output |
[1, 1, 1] | 3 |
[1, [1, 1]] | 3 |
[1, 2, [3, 4, [5, 6]], 7, [8, [9, 10]]] | 55 |
Explanation: In the above example, the list is nested to a depth of 3 in some places (e.g.,
[3, 4, [5, 6]]
) and 2 in others (e.g., [8, [9, 10]]
). The integers 1, 2, 3, 4, 5, 6, 7, 8, 9, and 10 all add up to 55.Constraints
Time limit: 2 seconds
Memory limit: 512 MB
Output limit: 1 MB