langs a-z
This commit is contained in:
parent
db842d013d
commit
d066446780
11389 changed files with 98361 additions and 1020 deletions
|
|
@ -0,0 +1,32 @@
|
|||
/* REXX ***************************************************************
|
||||
* 10.08.2012 Walter Pachl Pascal algorithm -> Rexx -> NetRexx
|
||||
**********************************************************************/
|
||||
s=' -1 -2 3 5 6 -2 -1 4 -4 2 -1'
|
||||
maxSum = 0
|
||||
seqStart = 0
|
||||
seqEnd = -1
|
||||
Loop i = 1 To s.words()
|
||||
seqSum = 0
|
||||
Loop j = i to s.words()
|
||||
seqSum = seqSum + s.word(j)
|
||||
if seqSum > maxSum then Do
|
||||
maxSum = seqSum
|
||||
seqStart = i
|
||||
seqEnd = j
|
||||
end
|
||||
end
|
||||
end
|
||||
Say 'Sequence:'
|
||||
Say s
|
||||
Say 'Subsequence with greatest sum: '
|
||||
If seqend<seqstart Then
|
||||
Say 'empty'
|
||||
Else Do
|
||||
ol=' '.copies(seqStart-1)
|
||||
Loop i = seqStart to seqEnd
|
||||
w=s.word(i)
|
||||
ol=ol||w.right(3)
|
||||
End
|
||||
Say ol
|
||||
Say 'Sum:' maxSum
|
||||
End
|
||||
|
|
@ -0,0 +1,17 @@
|
|||
let maxsubseq =
|
||||
let rec loop sum seq maxsum maxseq = function
|
||||
| [] -> maxsum, List.rev maxseq
|
||||
| x::xs ->
|
||||
let sum = sum + x
|
||||
and seq = x :: seq in
|
||||
if sum < 0 then
|
||||
loop 0 [] maxsum maxseq xs
|
||||
else if sum > maxsum then
|
||||
loop sum seq sum seq xs
|
||||
else
|
||||
loop sum seq maxsum maxseq xs
|
||||
in
|
||||
loop 0 [] 0 []
|
||||
|
||||
let _ =
|
||||
maxsubseq [-1 ; -2 ; 3 ; 5 ; 6 ; -2 ; -1 ; 4; -4 ; 2 ; -1]
|
||||
|
|
@ -0,0 +1,24 @@
|
|||
declare
|
||||
fun {MaxSubSeq Xs}
|
||||
|
||||
fun {Step [Sum0 Seq0 MaxSum MaxSeq] X}
|
||||
Sum = Sum0 + X
|
||||
Seq = X|Seq0
|
||||
in
|
||||
if Sum > MaxSum then
|
||||
%% found new maximum
|
||||
[Sum Seq Sum Seq]
|
||||
elseif Sum < 0 then
|
||||
%% discard negative subseqs
|
||||
[0 nil MaxSum MaxSeq]
|
||||
else
|
||||
[Sum Seq MaxSum MaxSeq]
|
||||
end
|
||||
end
|
||||
|
||||
[_ _ _ MaxSeq] = {FoldL Xs Step [0 nil 0 nil]}
|
||||
in
|
||||
{Reverse MaxSeq}
|
||||
end
|
||||
in
|
||||
{Show {MaxSubSeq [~1 ~2 3 5 6 ~2 ~1 4 ~4 2 1]}}
|
||||
|
|
@ -0,0 +1,14 @@
|
|||
grsub(v)={
|
||||
my(mn=1,mx=#v,r=0,at,c);
|
||||
if(vecmax(v)<=0,return([1,0]));
|
||||
while(v[mn]<=0,mn++);
|
||||
while(v[mx]<=0,mx--);
|
||||
for(a=mn,mx,
|
||||
c=0;
|
||||
for(b=a,mx,
|
||||
c+=v[b];
|
||||
if(c>r,r=c;at=[a,b])
|
||||
)
|
||||
);
|
||||
at
|
||||
};
|
||||
|
|
@ -0,0 +1,40 @@
|
|||
Program GreatestSubsequentialSum(output);
|
||||
|
||||
var
|
||||
a: array[1..11] of integer = (-1, -2, 3, 5, 6, -2, -1, 4, -4, 2, -1);
|
||||
i, j: integer;
|
||||
seqStart, seqEnd: integer;
|
||||
maxSum, seqSum: integer;
|
||||
|
||||
begin
|
||||
maxSum := 0;
|
||||
seqStart := 0;
|
||||
seqEnd := -1;
|
||||
for i := low(a) to high(a) do
|
||||
begin
|
||||
seqSum := 0;
|
||||
for j := i to high(a) do
|
||||
begin
|
||||
seqSum := seqSum + a[j];
|
||||
if seqSum > maxSum then
|
||||
begin
|
||||
maxSum := seqSum;
|
||||
seqStart := i;
|
||||
seqEnd := j;
|
||||
end;
|
||||
end;
|
||||
end;
|
||||
|
||||
writeln ('Sequence: ');
|
||||
for i := low(a) to high(a) do
|
||||
write (a[i]:3);
|
||||
writeln;
|
||||
writeln ('Subsequence with greatest sum: ');
|
||||
for i := low(a) to seqStart - 1 do
|
||||
write (' ':3);
|
||||
for i := seqStart to seqEnd do
|
||||
write (a[i]:3);
|
||||
writeln;
|
||||
writeln ('Sum:');
|
||||
writeln (maxSum);
|
||||
end.
|
||||
|
|
@ -0,0 +1,13 @@
|
|||
sub maxsubseq (*@a) {
|
||||
my ($start, $end, $sum, $maxsum) = -1, -1, 0, 0;
|
||||
for @a.kv -> $i, $x {
|
||||
$sum += $x;
|
||||
if $maxsum < $sum {
|
||||
($maxsum, $end) = $sum, $i;
|
||||
}
|
||||
elsif $sum < 0 {
|
||||
($sum, $start) = 0, $i;
|
||||
}
|
||||
}
|
||||
return @a[$start ^.. $end];
|
||||
}
|
||||
|
|
@ -0,0 +1,16 @@
|
|||
sub max_sub-seq ( *@a ) {
|
||||
|
||||
my $max_subset = [];
|
||||
while @a {
|
||||
my @subsets = @a.keys.map: { [ @a[0..$_] ] };
|
||||
@subsets.push($max_subset);
|
||||
$max_subset = @subsets.max: { [+] .list };
|
||||
@a.shift;
|
||||
}
|
||||
|
||||
return $max_subset;
|
||||
}
|
||||
|
||||
max_sub-seq( -1, -2, 3, 5, 6, -2, -1, 4, -4, 2, -1 ).perl.say;
|
||||
max_sub-seq( -2, -2, -1, 3, 5, 6, -1, 4, -4, 2, -1 ).perl.say;
|
||||
max_sub-seq( -2, -2, -1, -3, -5, -6, -1, -4, -4, -2, -1 ).perl.say;
|
||||
|
|
@ -0,0 +1,35 @@
|
|||
If OpenConsole()
|
||||
Define s$, a, b, p1, p2, sum, max, dm=(?EndOfMyData-?MyData)
|
||||
Dim Seq.i(dm/SizeOf(Integer))
|
||||
CopyMemory(?MyData,@seq(),dm)
|
||||
|
||||
For a=0 To ArraySize(seq())
|
||||
sum=0
|
||||
For b=a To ArraySize(seq())
|
||||
sum+seq(b)
|
||||
If sum>max
|
||||
max=sum
|
||||
p1=a
|
||||
p2=b
|
||||
EndIf
|
||||
Next
|
||||
Next
|
||||
|
||||
For a=p1 To p2
|
||||
s$+str(seq(a))
|
||||
If a<p2
|
||||
s$+"+"
|
||||
EndIf
|
||||
Next
|
||||
PrintN(s$+" = "+str(max))
|
||||
|
||||
Print("Press ENTER to quit"): Input()
|
||||
CloseConsole()
|
||||
EndIf
|
||||
|
||||
|
||||
DataSection
|
||||
MyData:
|
||||
Data.i -1, -2, 3, 5, 6, -2, -1, 4, -4, 2, -1
|
||||
EndOfMyData:
|
||||
EndDataSection
|
||||
|
|
@ -0,0 +1,18 @@
|
|||
seq$ = "-1, -2, 3, 5, 6, -2, -1, 4, -4, 2, -1"
|
||||
max = -999
|
||||
for i = 1 to 11
|
||||
sum = 0
|
||||
for j = i to 11
|
||||
sum = sum + val(word$(seq$,j,","))
|
||||
If sum > max then
|
||||
max = sum
|
||||
i1 = i
|
||||
j1 = j
|
||||
end if
|
||||
next j
|
||||
next i
|
||||
print "Sum:";
|
||||
for i = i1 to j1
|
||||
print word$(seq$,i,",");",";
|
||||
next i
|
||||
print " = ";max
|
||||
|
|
@ -0,0 +1,47 @@
|
|||
$ include "seed7_05.s7i";
|
||||
|
||||
const func array integer: maxSubseq (in array integer: sequence) is func
|
||||
result
|
||||
var array integer: maxSequence is 0 times 0;
|
||||
local
|
||||
var integer: number is 0;
|
||||
var integer: index is 0;
|
||||
var integer: currentSum is 0;
|
||||
var integer: currentStart is 1;
|
||||
var integer: maxSum is 0;
|
||||
var integer: startPos is 0;
|
||||
var integer: endPos is 0;
|
||||
begin
|
||||
for number key index range sequence do
|
||||
currentSum +:= number;
|
||||
if currentSum < 0 then
|
||||
currentStart := succ(index);
|
||||
currentSum := 0;
|
||||
elsif currentSum > maxSum then
|
||||
maxSum := currentSum;
|
||||
startPos := currentStart;
|
||||
endPos := index;
|
||||
end if;
|
||||
end for;
|
||||
if startPos <= endPos and startPos >= 1 and endPos >= 1 then
|
||||
maxSequence := sequence[startPos .. endPos];
|
||||
end if;
|
||||
end func;
|
||||
|
||||
const proc: main is func
|
||||
local
|
||||
const array integer: a1 is [] (-1, -2, 3, 5, 6, -2, -1, 4, -4, 2, -1);
|
||||
const array integer: a2 is [] (-1, -2, -3, -5, -6, -2, -1, -4, -4, -2, -1);
|
||||
var integer: number is 0;
|
||||
begin
|
||||
write("Maximal subsequence:");
|
||||
for number range maxSubseq(a1) do
|
||||
write(" " <& number);
|
||||
end for;
|
||||
writeln;
|
||||
write("Maximal subsequence:");
|
||||
for number range maxSubseq(a2) do
|
||||
write(" " <& number);
|
||||
end for;
|
||||
writeln;
|
||||
end func;
|
||||
|
|
@ -0,0 +1,18 @@
|
|||
val maxsubseq = let
|
||||
fun loop (_, _, maxsum, maxseq) [] = (maxsum, rev maxseq)
|
||||
| loop (sum, seq, maxsum, maxseq) (x::xs) = let
|
||||
val sum = sum + x
|
||||
val seq = x :: seq
|
||||
in
|
||||
if sum < 0 then
|
||||
loop (0, [], maxsum, maxseq) xs
|
||||
else if sum > maxsum then
|
||||
loop (sum, seq, sum, seq) xs
|
||||
else
|
||||
loop (sum, seq, maxsum, maxseq) xs
|
||||
end
|
||||
in
|
||||
loop (0, [], 0, [])
|
||||
end;
|
||||
|
||||
maxsubseq [~1, ~2, 3, 5, 6, ~2, ~1, 4, ~4, 2, ~1]
|
||||
|
|
@ -0,0 +1,8 @@
|
|||
#import std
|
||||
#import int
|
||||
|
||||
max_subsequence = zleq$^l&r/&+ *aayK33PfatPRTaq ^/~& sum:-0
|
||||
|
||||
#cast %zL
|
||||
|
||||
example = max_subsequence <-1,-2,3,5,6,-2,-1,4,-4,2,-1>
|
||||
|
|
@ -0,0 +1,24 @@
|
|||
include c:\cxpl\codes;
|
||||
int Array, Size, Sum, Best, I, Lo, Hi, BLo, BHi;
|
||||
|
||||
[Array:= [-1, -2, 3, 5, 6, -2, -1, 4, -4, 2, -1];
|
||||
Size:= 11;
|
||||
Best:= -100000;
|
||||
for Lo:= 0 to Size-1 do
|
||||
for Hi:= Lo to Size-1 do
|
||||
[Sum:= 0;
|
||||
for I:= Lo to Hi do
|
||||
Sum:= Sum + Array(I);
|
||||
if Sum > Best then
|
||||
[Best:= Sum; BLo:= Lo; BHi:= Hi];
|
||||
];
|
||||
Text(0, "Sequence = ");
|
||||
for I:= 0 to Size-1 do
|
||||
[IntOut(0, Array(I)); Text(0, " ")];
|
||||
CrLf(0);
|
||||
Text(0, "Greatest = ");
|
||||
for I:= BLo to BHi do
|
||||
[IntOut(0, Array(I)); Text(0, " ")];
|
||||
CrLf(0);
|
||||
Text(0, "Sum = "); IntOut(0, Best); CrLf(0);
|
||||
]
|
||||
Loading…
Add table
Add a link
Reference in a new issue