Skip to content

Instantly share code, notes, and snippets.

@yasith
Created April 12, 2012 04:10
Show Gist options
  • Save yasith/2364548 to your computer and use it in GitHub Desktop.
Save yasith/2364548 to your computer and use it in GitHub Desktop.
PrefixSums
1 def rec(L, p=0):
2 ans = []
3 if p > len(L):
4 return ans
5
6 ans.append(sum(L[:p]))
7 ans += rec(L, p+1)
8
9 return ans
11 def rec2(L, ans=[0]):
12 if L == []:
13 return ans
14 return rec2(L[1:], ans+[(L[0]+ans[-1])])
16 def rec3(L):
17 if L == []:
18 return [0]
19
20 ans = rec3(L[1:])
21
22 for i in range(len(ans)):
23 ans[i] += L[0]
24
25 return [0] + ans
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment