Last active
October 5, 2019 11:31
-
-
Save fonsp/20287ecdc4a2436bc278949eb3a76040 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 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