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 |
This REXX version will find the sum of the ''shortest greatest continuous subsequence''. |
||
<lang rexx>/*REXX program finds the shortest greatest |
<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) /*select one number at a time. */ |
|||
do k=j to w /* [↓] process 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 |
|||
⚫ | |||
if s>sum then do; sum=s; at=j; L=k-j+1; end |
|||
end /*j*/ |
|||
⚫ | |||
$=subword(@,at,L); if $=='' then $="[NULL]" /*Englishize it.*/ |
|||
say; |
say; say 'sum='sum/1 " sequence="$ /*stick a fork in it, we're done.*/</lang> |
||
⚫ | |||
⚫ | |||
⚫ | |||
<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: <tt>1 2 3 4 -777 1 2 3 4 0 0</tt> |
{{out}} when the following was used for the list: <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 |
This REXX version will find the sum of the ''longest greatest continuous subsequence''. |
||
<lang rexx>/*REXX program finds the longest greatest |
<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; _=k-j+1 /* [↓] process 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 |
|||
⚫ | |||
_=k-j+1 |
|||
⚫ | |||
if (s==sum & _>L) | s>sum then do; sum=s; at=j; L=_; end |
|||
end /*k*/ |
|||
end /*j*/ |
|||
$=subword(@,at,L); if $=='' then $="[NULL]" /*Englishize it.*/ |
|||
say; |
say; say 'sum='sum/1 " sequence="$ /*stick a fork in it, we're done.*/</lang> |
||
⚫ | |||
⚫ | |||
⚫ | |||
<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 |