September Morn Update
This commit is contained in:
parent
4e2d22a71d
commit
aac6731f2c
6856 changed files with 141342 additions and 21127 deletions
|
|
@ -0,0 +1,21 @@
|
|||
'''Greatest subsequential sum'''
|
||||
|
||||
from functools import (reduce)
|
||||
|
||||
|
||||
# maxSubseq :: [Int] -> [Int] -> (Int, [Int])
|
||||
def maxSubseq(xs):
|
||||
'''Subsequence of xs with the maximum sum'''
|
||||
def go(ab, x):
|
||||
(m1, m2) = ab[0]
|
||||
hi = max((0, []), (m1 + x, m2 + [x]))
|
||||
return (hi, max(ab[1], hi))
|
||||
return reduce(go, xs, ((0, []), (0, [])))[1]
|
||||
|
||||
|
||||
# TEST -----------------------------------------------------------
|
||||
print(
|
||||
maxSubseq(
|
||||
[-1, -2, 3, 5, 6, -2, -1, 4, -4, 2, -1]
|
||||
)
|
||||
)
|
||||
Loading…
Add table
Add a link
Reference in a new issue