Data update

This commit is contained in:
Ingy döt Net 2025-06-11 20:16:52 -04:00
parent 72eb4943cb
commit 4d5544505c
2347 changed files with 62432 additions and 16731 deletions

View file

@ -0,0 +1,42 @@
import ballerina/io;
function gss(int[] s) returns [int[], int] {
int best = 0;
int strt = 0;
int end = 0;
int sum = 0;
int sumStart = 0;
foreach var [i, x] in s.enumerate() {
sum += x;
if sum > best {
best = sum;
strt = sumStart;
end = i + 1;
} else if sum < 0 {
sum = 0;
sumStart = i + 1;
}
}
return [s.slice(strt, end), best];
}
function A(any[] a) returns string {
return re `,`.replaceAll(a.toString(), ", ");
}
public function main() {
var tests = [
[-1, -2, 3, 5, 6, -2, -1, 4, -4, 2, -1],
[-1, 1, 2, -5, -6],
[],
[-1, -2, -1]
];
foreach var test in tests {
io:println("Input: ", A(test));
var res = gss(test);
var subSeq = res[0];
int sum = res[1];
io:println("Sub seq: ", A(subSeq));
io:println("Sum: ", sum, "\n");
}
}

View file

@ -1,4 +1,4 @@
proc max_subseq . seq[] start stop maxsum .
proc max_subseq &seq[] &start &stop &maxsum .
maxsum = 0
i = 1
start = 1
@ -18,6 +18,4 @@ proc max_subseq . seq[] start stop maxsum .
a[] = [ -1 -2 3 5 6 -2 -1 4 -4 2 -1 ]
max_subseq a[] a b sum
print "Max sum = " & sum
for i = a to b
write a[i] & " "
.
for i = a to b : write a[i] & " "

View file

@ -0,0 +1,26 @@
module Greatest_subsequential_sum {
showGSS=lambda (a)->{
function Greatest_subsequential_sum(a as array){
s=a#sum()
=a
p=len(a)-1
if p<0 then exit
for i=0 to p
for j=i to p
k= a#slice(i, j)
m=k#sum()
if m>s then =k: s=m
next
next
}
= "("+a#str$(",")+") -> ("+ Greatest_subsequential_sum(a)#str$(",")+")"
}
open "out.txt" for output as#f
print #f, showGSS((-1 , 2 , -1))
print #f, showGSS((-1, 2, -1, 3, -1))
print #f, showGSS((-1, 1, 2, -5, -6))
print #f, showGSS((-1 , -2 , 3 , 5 , 6 , -2 , -1 , 4 , -4 , 2 , -1))
close #f
win notepad, dir$+"out.txt"
}
Greatest_subsequential_sum

View file

@ -0,0 +1,36 @@
module Greatest_subsequential_sum (filename) {
showGSS=lambda (a)->{
function Greatest_subsequential_sum(a as array){
var best = 0
var start = 0
var end = 0
var sum = 0
var sumStart = 0
var i = 0
s=each(a)
while s {
sum +=array(s)
if (sum > best) then {
best = sum
start = sumStart
end = i + 1
} else.if (sum < 0) then {
sum = 0
sumStart = i + 1
}
i++
}
=a#slice(start, end-1)
}
= "("+a#str$(",")+") -> ("+ Greatest_subsequential_sum(a)#str$(",")+")"
}
open filename for output as#f
print #f, showGSS((-1 , 2 , -1))
print #f, showGSS((-1, 2, -1, 3, -1))
print #f, showGSS((-1, 1, 2, -5, -6))
print #f, showGSS((-1 , -2 , 3 , 5 , 6 , -2 , -1 , 4 , -4 , 2 , -1))
close #f
if filename<>"" then win notepad, dir$+filename
}
Greatest_subsequential_sum "" ' to screen
Greatest_subsequential_sum "out.txt"