Last active
August 29, 2015 14:08
-
-
Save daeyun/5071506bb9439db63d58 to your computer and use it in GitHub Desktop.
This file contains 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 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