Skip to content

Instantly share code, notes, and snippets.

@K0NRAD
Created August 30, 2020 15:40
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save K0NRAD/76295b20a7e214d99baf8f6e1ed94900 to your computer and use it in GitHub Desktop.
Save K0NRAD/76295b20a7e214d99baf8f6e1ed94900 to your computer and use it in GitHub Desktop.
sum values of an array with subarrays
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