SHARE
TWEET

Untitled

a guest Oct 23rd, 2019 74 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  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]]
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
Not a member of Pastebin yet?
Sign Up, it unlocks many cool features!
 
Top