Created
August 30, 2020 15:40
-
-
Save K0NRAD/76295b20a7e214d99baf8f6e1ed94900 to your computer and use it in GitHub Desktop.
sum values of an array with subarrays
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def deep_sum(elem): | |
result = 0 | |
for item in elem: | |
if hasattr(item, '__len__'): | |
result += deep_sum(item) | |
else: | |
result += item | |
return result | |
assert deep_sum([1, -5, [50, 50, [100, 100, [2, 2]]], 100]) == 400 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment