September Morn Update
This commit is contained in:
parent
4e2d22a71d
commit
aac6731f2c
6856 changed files with 141342 additions and 21127 deletions
|
|
@ -0,0 +1,33 @@
|
|||
gsss(list l, integer &start, &end, &maxsum)
|
||||
{
|
||||
integer e, f, i, sum;
|
||||
|
||||
end = f = maxsum = start = sum = 0;
|
||||
for (i, e in l) {
|
||||
sum += e;
|
||||
if (sum < 0) {
|
||||
sum = 0;
|
||||
f = i + 1;
|
||||
} elif (maxsum < sum) {
|
||||
maxsum = sum;
|
||||
end = i + 1;
|
||||
start = f;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
main(void)
|
||||
{
|
||||
integer start, end, sum;
|
||||
list l;
|
||||
|
||||
l = list(-1, -2, 3, 5, 6, -2, -1, 4, -4, 2, -1);
|
||||
gsss(l, start, end, sum);
|
||||
o_("Max sum ", sum, "\n");
|
||||
if (start < end) {
|
||||
l.ocall(o_, 1, start, end - 1, " ");
|
||||
o_newline();
|
||||
}
|
||||
|
||||
0;
|
||||
}
|
||||
|
|
@ -0,0 +1,89 @@
|
|||
-- maxSubseq :: [Int] -> [Int] -> (Int, [Int])
|
||||
on maxSubseq(xs)
|
||||
script go
|
||||
on |λ|(ab, x)
|
||||
set a to fst(ab)
|
||||
set {m1, m2} to {fst(a), snd(a)}
|
||||
set high to max(Tuple(0, {}), Tuple(m1 + x, m2 & {x}))
|
||||
Tuple(high, max(snd(ab), high))
|
||||
end |λ|
|
||||
end script
|
||||
|
||||
snd(foldl(go, Tuple(Tuple(0, {}), Tuple(0, {})), xs))
|
||||
end maxSubseq
|
||||
|
||||
|
||||
-- TEST ---------------------------------------------------
|
||||
on run
|
||||
set mx to maxSubseq({-1, -2, 3, 5, 6, -2, -1, 4, -4, 2, -1})
|
||||
{fst(mx), snd(mx)}
|
||||
end run
|
||||
|
||||
|
||||
-- GENERIC ABSTRACTIONS -----------------------------------
|
||||
|
||||
-- foldl :: (a -> b -> a) -> a -> [b] -> a
|
||||
on foldl(f, startValue, xs)
|
||||
tell mReturn(f)
|
||||
set v to startValue
|
||||
set lng to length of xs
|
||||
repeat with i from 1 to lng
|
||||
set v to |λ|(v, item i of xs, i, xs)
|
||||
end repeat
|
||||
return v
|
||||
end tell
|
||||
end foldl
|
||||
|
||||
-- gt :: Ord a => a -> a -> Bool
|
||||
on gt(x, y)
|
||||
set c to class of x
|
||||
if record is c or list is c then
|
||||
fst(x) > fst(y)
|
||||
else
|
||||
x > y
|
||||
end if
|
||||
end gt
|
||||
|
||||
-- fst :: (a, b) -> a
|
||||
on fst(tpl)
|
||||
if class of tpl is record then
|
||||
|1| of tpl
|
||||
else
|
||||
item 1 of tpl
|
||||
end if
|
||||
end fst
|
||||
|
||||
-- Lift 2nd class handler function into 1st class script wrapper
|
||||
-- mReturn :: First-class m => (a -> b) -> m (a -> b)
|
||||
on mReturn(f)
|
||||
if class of f is script then
|
||||
f
|
||||
else
|
||||
script
|
||||
property |λ| : f
|
||||
end script
|
||||
end if
|
||||
end mReturn
|
||||
|
||||
-- max :: Ord a => a -> a -> a
|
||||
on max(x, y)
|
||||
if gt(x, y) then
|
||||
x
|
||||
else
|
||||
y
|
||||
end if
|
||||
end max
|
||||
|
||||
-- snd :: (a, b) -> b
|
||||
on snd(tpl)
|
||||
if class of tpl is record then
|
||||
|2| of tpl
|
||||
else
|
||||
item 2 of tpl
|
||||
end if
|
||||
end snd
|
||||
|
||||
-- Tuple (,) :: a -> b -> (a, b)
|
||||
on Tuple(a, b)
|
||||
{type:"Tuple", |1|:a, |2|:b, length:2}
|
||||
end Tuple
|
||||
|
|
@ -1,6 +1,8 @@
|
|||
maxsubseq = snd . foldl f ((0,[]),(0,[])) where
|
||||
f ((h1,h2),sofar) x = (a,b) where
|
||||
a = max (0,[]) (h1 + x, h2 ++ [x])
|
||||
b = max sofar a
|
||||
maxSubseq :: [Int] -> (Int, [Int])
|
||||
maxSubseq =
|
||||
let go x ((h1, h2), sofar) =
|
||||
((,) <*> max sofar) (max (0, []) (h1 + x, x : h2))
|
||||
in snd . foldr go ((0, []), (0, []))
|
||||
|
||||
main = print $ maxsubseq [-1, -2, 3, 5, 6, -2, -1, 4, -4, 2, -1]
|
||||
main :: IO ()
|
||||
main = print $ maxSubseq [-1, -2, 3, 5, 6, -2, -1, 4, -4, 2, -1]
|
||||
|
|
|
|||
|
|
@ -0,0 +1,59 @@
|
|||
(() => {
|
||||
|
||||
// maxSubseq :: [Int] -> (Int, [Int])
|
||||
const maxSubseq = xs =>
|
||||
snd(xs.reduce((tpl, x) => {
|
||||
const [m1, m2] = Array.from(fst(tpl)),
|
||||
high = max(
|
||||
Tuple(0, []),
|
||||
Tuple(m1 + x, m2.concat(x))
|
||||
);
|
||||
return Tuple(high, max(snd(tpl), high));
|
||||
}, Tuple(Tuple(0, []), Tuple(0, []))));
|
||||
|
||||
|
||||
// TEST -----------------------------------------------
|
||||
// main :: IO ()
|
||||
const main = () => {
|
||||
const mx = maxSubseq([-1, -2, 3, 5, 6, -2, -1, 4, -4, 2, -1]);
|
||||
showLog(snd(mx), fst(mx))
|
||||
}
|
||||
// [3,5,6,-2,-1,4] -> 15
|
||||
|
||||
|
||||
// GENERIC FUNCTIONS ----------------------------------
|
||||
|
||||
// fst :: (a, b) -> a
|
||||
const fst = tpl => tpl[0];
|
||||
|
||||
// gt :: Ord a => a -> a -> Bool
|
||||
const gt = (x, y) =>
|
||||
'Tuple' === x.type ? (
|
||||
x[0] > y[0]
|
||||
) : (x > y);
|
||||
|
||||
// max :: Ord a => a -> a -> a
|
||||
const max = (a, b) => gt(b, a) ? b : a;
|
||||
|
||||
// showLog :: a -> IO ()
|
||||
const showLog = (...args) =>
|
||||
console.log(
|
||||
args
|
||||
.map(JSON.stringify)
|
||||
.join(' -> ')
|
||||
);
|
||||
|
||||
// snd :: (a, b) -> b
|
||||
const snd = tpl => tpl[1];
|
||||
|
||||
// Tuple (,) :: a -> b -> (a, b)
|
||||
const Tuple = (a, b) => ({
|
||||
type: 'Tuple',
|
||||
'0': a,
|
||||
'1': b,
|
||||
length: 2
|
||||
});
|
||||
|
||||
// MAIN ---
|
||||
return main();
|
||||
})();
|
||||
|
|
@ -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