Skip to content

Instantly share code, notes, and snippets.

@daeyun
Last active August 29, 2015 14:08
Show Gist options
  • Save daeyun/5071506bb9439db63d58 to your computer and use it in GitHub Desktop.
Save daeyun/5071506bb9439db63d58 to your computer and use it in GitHub Desktop.
def levelsum(L, k):
if L.__class__ == int:
return L
total = 0
for element in L:
total += level_sum(element, k+1)
if k == 0:
return total
return total * (k+1) / float(k)
a = [1, 2, [3, 4, [5]]]
b = [[[[7]]]]
c = []
assert(level_sum(a, 0) == 32)
assert(level_sum(b, 0) == 7*4)
assert(level_sum(c, 0) == 0)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment