Skip to content

Instantly share code, notes, and snippets.

@fonsp
Last active October 5, 2019 11:31
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 fonsp/20287ecdc4a2436bc278949eb3a76040 to your computer and use it in GitHub Desktop.
Save fonsp/20287ecdc4a2436bc278949eb3a76040 to your computer and use it in GitHub Desktop.
def g(input, offset=0):
return [sum(input[:i+1]) + offset if x else None for i, x in enumerate(input)]
def f(input, offset_carry=0):
# ~~ recursive ~~
# base case:
if not input:
return []
# recursive case:
current = input[0]
current_sum = sum(current)
# e.g. [[1, 2, None]] + [[3, None], [None, 4, 5]]
return [g(current, offset_carry)] + f(input[1:], offset_carry + current_sum)
wow = [[True, False, True, True], [False, False, False, True]]
print(f(wow))
# prints:
# [[1, None, 2, 3], [None, None, None, 4]]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment