Created
April 12, 2012 04:10
-
-
Save yasith/2364548 to your computer and use it in GitHub Desktop.
PrefixSums
This file contains hidden or 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
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