This commit is contained in:
Ingy döt Net 2013-04-10 21:29:02 -07:00
parent 764da6cbbb
commit db842d013d
19005 changed files with 197040 additions and 7 deletions

View file

@ -0,0 +1,20 @@
/*REXX program finds the shortest greatest continous subsequence sum.*/
arg @ /*get the arugment LIST (if any).*/
say 'words='words(@) ' list='@ /*show WORDS and LIST to console.*/
sum=word(@,1) /*a "starter" sum (of a sequence)*/
w=words(@) /*number of words in the list. */
at=1 /*where the sequence starts at. */
L=0 /*the length of the sequence. */
/*process the list. */
do j=1 for w; f=word(@,j)
do k=j to w; s=f
do m=j+1 to k
s=s+word(@,m)
end /*m*/
if s>sum then do; sum=s; at=j; L=k-j+1; end
end /*k*/
end /*j*/
seq=subword(@,at,L); if seq=='' then seq="[NULL]"
say; say 'sum='word(sum 0,1)/1 " sequence="seq
/*stick a fork in it, we're done.*/

View file

@ -0,0 +1,21 @@
/*REXX program finds the longest greatest continous subsequence sum. */
arg @ /*get the arugment LIST (if any).*/
say 'words='words(@) ' list='@ /*show WORDS and LIST to console.*/
sum=word(@,1) /*a "starter" sum (of a sequence)*/
w=words(@) /*number of words in the list. */
at=1 /*where the sequence starts at. */
L=0 /*the length of the sequence. */
/*process the list. */
do j=1 for w; f=word(@,j)
do k=j to w; s=f
do m=j+1 to k
s=s+word(@,m)
end /*m*/
_=k-j+1
if (s==sum & _>L) | s>sum then do; sum=s; at=j; L=_; end
end /*k*/
end /*j*/
seq=subword(@,at,L); if seq=='' then seq="[NULL]"
say; say 'sum='word(sum 0,1)/1 " sequence="seq
/*stick a fork in it, we're done.*/

View file

@ -0,0 +1,31 @@
/* REXX ***************************************************************
* 09.08.2012 Walter Pachl translated Pascal algorithm to Rexx
**********************************************************************/
s=' -1 -2 3 5 6 -2 -1 4 -4 2 -1'
maxSum = 0
seqStart = 0
seqEnd = -1
do i = 1 To words(s)
seqSum = 0
Do j = i to words(s)
seqSum = seqSum + word(s,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)
Do i = seqStart to seqEnd
ol=ol||right(word(s,i),3)
End
Say ol
Say 'Sum:' maxSum
End