Data update

This commit is contained in:
Ingy döt Net 2024-10-16 18:07:41 -07:00
parent 81fd053722
commit 52a6ef48dd
10248 changed files with 63654 additions and 6775 deletions

View file

@ -1,4 +1,3 @@
main:
(
[]INT a = (-1 , -2 , 3 , 5 , 6 , -2 , -1 , 4 , -4 , 2 , -1);
@ -23,7 +22,7 @@ main:
OD;
FOR i FROM begin max TO end max DO
print(a[i])
print((" ",whole(a[i],0)))
OD
)

View file

@ -0,0 +1,64 @@
_rndlen = 01 //BOOL: is sequence length random or fixed
_maxlen = 14 //Length of each/longest sequence
_maxVal = 20 //Largest number (pos or neg) in sequence
_repeat = 25 //Number of sequences to evaluate
void local fn maxSubSequence
int i, j, seq(_maxlen), ndx(_maxlen + 1),sum(_maxlen)
int grp, maxSum, maxNdx, maxEnd, tot, iterations = _repeat
while iterations
iterations--
// Create random sequence
int count = (_rndlen) ? rnd(_maxlen) : _maxlen
for i = 1 to count
seq(i) = rnd(_maxVal * 2 + 1) - _maxVal - 1
next
// Determine maximum sub-sequence
bool isNeg = yes : grp = 0 : sum(0) = 0 : ndx(0) = 0
maxSum = 0 : maxNdx = 0 : maxEnd = 0
for i = 1 to count // Merge array into groups of like signs
if (seq(i) < 0) <> isNeg // If change of sign...
grp ++ // Start new group
isNeg = yes - isNeg
ndx(grp) = i
sum(grp) = 0
end if
sum(grp) += seq(i)
next
ndx(grp + 1) = 0
for i = 1 to grp step 2 // Determine max sub-sequence
j = i : tot = sum(j)
do
if tot > maxSum
maxSum = tot
maxNdx = ndx(i)
maxEnd = ndx(j + 1)
end if
j += 2 : tot += sum(j - 1) + sum(j) // add next neg & pos groups
until j > grp
next
// Print result
print @" Sum = "; maxSum, " {";
for i = 1 to count
if i == maxNdx then text ,,, _zCyan
if i == maxEnd then text ,,, fn ColorClear
print " "; seq(i); " ";
next
text ,,, fn ColorClear
print "}"
wend
end fn
window 1, @"Maximum subsequence"
CFTimeInterval t : t = fn CACurrentMediaTime
fn maxSubSequence
printf @"\n %d random sequences created, solved, and printed in %.3f sec.",_repeat,1*(fn CACurrentMediaTime - t)
handleevents

View file

@ -0,0 +1,28 @@
function MaxSumSeq(a: array of integer): (integer,integer,integer);
begin
var (maxSum,thisSum) := (0,0);
var (f,t) := (0,-1);
var k := 0;
for var j:=0 to a.Length-1 do
begin
thisSum += a[j];
if thisSum < 0 then
begin
k := j + 1;
thisSum := 0;
end
else if thisSum > maxSum then
begin
maxSum := thisSum;
f := k;
t := j
end;
end;
Result := (f,t,maxSum);
end;
begin
var a := Arr(-1 , -2 , 3 , 5 , 6 , -2 , -1 , 4 , -4 , 2 , -1);
var (f,t,max) := MaxSumSeq(a);
Print('Subsequence with max sum:', a[f:t+1], 'It''s sum:', max);
end.