Greatest subsequential sum: Difference between revisions

Content deleted Content added
m {{out}}
→‎{{header|REXX}}: changed/add comments and whitespace, aligned structure and statements better.
Line 2,092: Line 2,092:
=={{header|REXX}}==
=={{header|REXX}}==
===shortest greatest subsequential sum===
===shortest greatest subsequential sum===
This REXX version will find the sum of the shortest greatest continous subsequence.
This REXX version will find the   sum   of the   ''shortest greatest continuous subsequence''.
<lang rexx>/*REXX program finds the shortest greatest continous subsequence sum.*/
<lang rexx>/*REXX program finds the shortest greatest continuous subsequence sum.*/
parse arg @; w=words(@) /*get arg list; # words in list.*/
parse arg @; w=words(@) /*get arg list; # words in list.*/
say 'words='w " list="@ /*show #words & LIST to console.*/
say 'words='w " list="@ /*show #words & LIST to console.*/
sum=0; L=0; at=w+1 /*default sum, length, starts at.*/
sum=0; L=0; at=w+1 /*default sum, length, starts at.*/
/* [↓] process the list. */
/* [↓] process the list of nums.*/
do j=1 for w; f=word(@,j)
do j=1 for w; f=word(@,j) /*select one number at a time. */
do k=j to w; s=f /* [↓] process the sub-list. */
do k=j to w /* [↓] process a sub─list of #s.*/
do m=j+1 to k; s=s+word(@,m)
s=f; do m=j+1 to k; s=s+word(@,m); end /*m*/
end /*m*/
if s>sum then do; sum=s; at=j; L=k-j+1; end
end /*k*/ /* [↑] chose greatest sum of #s.*/
if s>sum then do; sum=s; at=j; L=k-j+1; end
end /*k*/
end /*j*/
end /*j*/


seq=subword(@,at,L); if seq=='' then seq="[NULL]"
$=subword(@,at,L); if $=='' then $="[NULL]" /*Englishize it.*/
say; say 'sum='word(sum 0,1)/1 " sequence="seq
say; say 'sum='sum/1 " sequence="$ /*stick a fork in it, we're done.*/</lang>
{{out}} when the following was used for the list: &nbsp; <tt> -1 -2 3 5 6 -2 -1 4 -4 2 -1 </tt>
/*stick a fork in it, we're done.*/</lang>
{{out}} when the following was used for the list: &nbsp; <tt>-1 -2 3 5 6 -2 -1 4 -4 2 -1</tt>
<pre>
<pre>
words=11 list=-1 -2 3 5 6 -2 -1 4 -4 2 -1
words=11 list=-1 -2 3 5 6 -2 -1 4 -4 2 -1
Line 2,115: Line 2,113:
sum=15 sequence=3 5 6 -2 -1 4
sum=15 sequence=3 5 6 -2 -1 4
</pre>
</pre>
{{out}} when the following was used for the list: &nbsp; <tt>1 2 3 4 -777 1 2 3 4 0 0</tt>
{{out}} when the following was used for the list: &nbsp; <tt> 1 2 3 4 -777 1 2 3 4 0 0 </tt>
<pre>
<pre>
words=12 list=1 2 3 4 0 -777 1 2 3 4 0 0
words=12 list=1 2 3 4 0 -777 1 2 3 4 0 0
Line 2,123: Line 2,121:


===longest greatest subsequential sum===
===longest greatest subsequential sum===
This REXX version will find the sum of the longest greatest continous subsequence.
This REXX version will find the &nbsp; sum &nbsp; of the &nbsp; ''longest greatest continuous subsequence''.
<lang rexx>/*REXX program finds the longest greatest continous subsequence sum. */
<lang rexx>/*REXX program finds the longest greatest continuous subsequence sum.*/
parse arg @; w=words(@) /*get arg list; # words in list.*/
parse arg @; w=words(@) /*get arg list; # words in list.*/
say 'words='w " list="@ /*show #words & LIST to console.*/
say 'words='w " list="@ /*show #words & LIST to console.*/
sum=0; L=0; at=w+1 /*default sum, length, starts at.*/
sum=0; L=0; at=w+1 /*default sum, length, starts at.*/
/* [↓] process the list. */
/* [↓] process the list of nums.*/
do j=1 for w; f=word(@,j)
do j=1 for w; f=word(@,j) /*select one number at a time. */
do k=j to w; s=f /* [↓] process the sub-list. */
do k=j to w; _=k-j+1 /* [↓] process a sub─list of #s.*/
do m=j+1 to k; s=s+word(@,m)
s=f; do m=j+1 to k; s=s+word(@,m); end /*m*/
end /*m*/
if (s==sum & _>L) | s>sum then do; sum=s; at=j; L=_; end
end /*k*/ /* [↑] chose longest greatest sum*/
_=k-j+1
end /*j*/
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]"
$=subword(@,at,L); if $=='' then $="[NULL]" /*Englishize it.*/
say; say 'sum='word(sum 0,1)/1 " sequence="seq
say; say 'sum='sum/1 " sequence="$ /*stick a fork in it, we're done.*/</lang>
{{out}} when the following was used for the list: &nbsp; <tt> 1 2 3 4 -777 1 2 3 4 0 0 </tt>
/*stick a fork in it, we're done.*/</lang>
{{out}} when the following was used for the list: &nbsp; <tt>1 2 3 4 -777 1 2 3 4 0 0</tt>
<pre>
<pre>
words=12 list=1 2 3 4 0 -777 1 2 3 4 0 0
words=12 list=1 2 3 4 0 -777 1 2 3 4 0 0