This commit is contained in:
Ingy döt Net 2013-10-27 22:24:23 +00:00
parent 6f050a029e
commit 776bba907c
3887 changed files with 59894 additions and 7280 deletions

View file

@ -3,32 +3,35 @@ import std.stdio, std.algorithm, std.range, std.typecons;
mixin template InitsTails(T) {
T[] data;
size_t pos;
@property bool empty() { return pos > data.length; }
void popFront() { pos++; }
@property bool empty() pure nothrow {
return pos > data.length;
}
void popFront() pure nothrow { pos++; }
}
struct Inits(T) {
mixin InitsTails!T;
@property T[] front() { return data[0 .. pos]; }
@property T[] front() pure nothrow { return data[0 .. pos]; }
}
auto inits(T)(T[] seq) { return seq.Inits!T; }
struct Tails(T) {
mixin InitsTails!T;
@property T[] front() { return data[pos .. $]; }
@property T[] front() pure nothrow { return data[pos .. $]; }
}
auto tails(T)(T[] seq) { return seq.Tails!T; }
auto tails(T)(T[] seq) pure nothrow { return seq.Tails!T; }
auto maxSubseq(T)(T[] seq) /*pure nothrow*/ {
//return seq.tails.map!inits.join.reduce!(max!q{ a.sum });
return seq
.tails
.map!inits
.join
.map!q{ tuple(reduce!q{a + b}(0, a), a) }
.reduce!max[1];
T[] maxSubseq(T)(T[] seq) pure nothrow {
//return seq.tails.map!inits.join.reduce!(max!sum);
return reduce!max(tuple(0, T[].init),
seq
.tails
.map!inits
.join
.map!q{ tuple(reduce!q{a + b}(0, a), a) }
)[1];
}
void main() {

View file

@ -0,0 +1,14 @@
function [S,GS]=gss(a)
% Greatest subsequential sum
a =[0;a(:);0]';
ix1 = find(a(2:end) >0 & a(1:end-1) <= 0);
ix2 = find(a(2:end)<=0 & a(1:end-1) > 0);
K = 0;
S = 0;
for k = 1:length(ix1)
s = sum(a(ix1(k)+1:ix2(k)));
if (s>S)
S=s; K=k;
end;
end;
GS = a(ix1(K)+1:ix2(K));

View file

@ -0,0 +1,41 @@
*process source attributes xref;
ss: Proc Options(Main);
/* REXX ***************************************************************
* 26.08.2013 Walter Pachl translated from REXX version 3
**********************************************************************/
Dcl HBOUND builtin;
Dcl SYSPRINT Print;
Dcl (I,J,LB,MAXSUM,SEQEND,SEQSTART,SEQSUM) Bin Fixed(15);
Dcl s(11) Bin Fixed(15) Init(-1,-2,3,5,6,-2,-1,4,-4,2,-1);
maxSum = 0;
seqStart = 0;
seqEnd = -1;
do i = 1 To hbound(s);
seqSum = 0;
Do j = i to hbound(s);
seqSum = seqSum + s(j);
if seqSum > maxSum then Do;
maxSum = seqSum;
seqStart = i;
seqEnd = j;
end;
end;
end;
Put Edit('Sequence:')(Skip,a);
Put Edit('')(Skip,a);
Do i=1 To hbound(s);
Put Edit(s(i))(f(3));
End;
Put Edit('Subsequence with greatest sum:')(Skip,a);
If seqend<seqstart Then
Put Edit('empty')(Skip,a);
Else Do;
/*ol=copies(' ',seqStart-1)*/
lb=(seqStart-1)*3;
Put Edit(' ')(Skip,a(lb));
Do i = seqStart to seqEnd;
Put Edit(s(i))(f(3));
End;
Put Edit('Sum:',maxSum)(Skip,a,f(5));
End;
End;