Greatest subsequential sum: Difference between revisions

→‎{{header|REXX}}: changed/add comments and whitespace, aligned structure and statements better.
m ({{out}})
(→‎{{header|REXX}}: changed/add comments and whitespace, aligned structure and statements better.)
Line 2,092:
=={{header|REXX}}==
===shortest greatest subsequential sum===
This REXX version will find the   sum   of the   ''shortest greatest continouscontinuous subsequence''.
<lang rexx>/*REXX program finds the shortest greatest continouscontinuous subsequence sum.*/
parse arg @; w=words(@) /*get arg list; # words in list.*/
say 'words='w " list="@ /*show #words & LIST to console.*/
sum=0; L=0; at=w+1 /*default sum, length, starts at.*/
/* [↓] process the list. of nums.*/
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. a sub─list of #s.*/
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 /*m*/
end /*k*/ /*stick a[↑] fork inchose it,greatest we'resum doneof #s.*/</lang>
if s>sum then do; sum=s; at=j; L=k-j+1; end
end end /*kj*/
end /*j*/
 
seq$=subword(@,at,L); if seq$=='' then seq$="[NULL]" /*Englishize it.*/
say; say 'sum='word(sum 0,1)/1 " sequence="seq$ /*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>
words=11 list=-1 -2 3 5 6 -2 -1 4 -4 2 -1
Line 2,115 ⟶ 2,113:
sum=15 sequence=3 5 6 -2 -1 4
</pre>
{{out}} when the following was used for the list: &nbsp; <tt> 1 2 3 4 -777 1 2 3 4 0 0 </tt>
<pre>
words=12 list=1 2 3 4 0 -777 1 2 3 4 0 0
Line 2,123 ⟶ 2,121:
 
===longest greatest subsequential sum===
This REXX version will find the &nbsp; sum &nbsp; of the &nbsp; ''longest greatest continouscontinuous subsequence''.
<lang rexx>/*REXX program finds the longest greatest continouscontinuous subsequence sum. */
parse arg @; w=words(@) /*get arg list; # words in list.*/
say 'words='w " list="@ /*show #words & LIST to console.*/
sum=0; L=0; at=w+1 /*default sum, length, starts at.*/
/* [↓] process the list. of nums.*/
do j=1 for w; f=word(@,j) /*select one number at a time. */
do k=j to w; s_=f k-j+1 /* [↓] process the sub-list. a sub─list of #s.*/
s=f; do m=j+1 to k; s=s+word(@,m); end /*m*/
if (s==sum & _>L) | s>sum then do; sum=s; at=j; L=_; end /*m*/
end /*k*/ /*stick a[↑] forkchose inlongest it,greatest we're done.sum*/</lang>
_=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]" /*Englishize it.*/
say; say 'sum='word(sum 0,1)/1 " sequence="seq$ /*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>
words=12 list=1 2 3 4 0 -777 1 2 3 4 0 0