Advertisement
Guest User

Untitled

a guest
Oct 23rd, 2019
106
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.56 KB | None | 0 0
  1. def g(input, offset=0):
  2. return [sum(input[:i+1]) + offset if x else None for i, x in enumerate(input)]
  3.  
  4. def f(input, offset_carry=0):
  5. # ~~ recursive ~~
  6. # base case:
  7. if not input:
  8. return []
  9. # recursive case:
  10. current = input[0]
  11. current_sum = sum(current)
  12. # e.g. [[1, 2, None]] + [[3, None], [None, 4, 5]]
  13. return [g(current, offset_carry)] + f(input[1:], offset_carry + current_sum)
  14.  
  15.  
  16. wow = [[True, False, True, True], [False, False, False, True]]
  17.  
  18. print(f(wow))
  19.  
  20. # prints:
  21. # [[1, None, 2, 3], [None, None, None, 4]]
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement