Let's explore a closely related but simpler problem: finding the sum of numbers in a list that may contain numbers or lists that may contain lists or numbers—you get the idea!
Exercise: Try to write a function that finds the sum of numbers in the following list.
[ 27, 38, 22, [42, [[18, 47, [13, 49, [26, 16, [[43, [8, 16, 35, 31, 46], 12], 19], 14, [, 36, 40, 44]]]]], [23, [38, 26, 5, 14], 25, 8, 12], 24, 39]]
Hint: in Python, you can check if a value is an integer with
You can check if that value is a list with
Hint: try solving simpler problems first. What is the sum of a list that contains only numbers? What is the sum of a list that contains only lists that contain only numbers?
The correct answer is 883 (hover to view.)
Don't spend more than 20 minutes on this. When you're ready to move on, click here. Don't worry; no spoilers on that page!