Greatest subsequential sum: Difference between revisions
Content deleted Content added
m added whitespace before the TOC. |
m →{{header|REXX}}: added/changed comments and whitespace, changed indentations. |
||
Line 2,476: | Line 2,476: | ||
===shortest greatest subsequential sum=== |
===shortest greatest subsequential sum=== |
||
This REXX version will find the sum of the ''shortest greatest continuous subsequence''. |
This REXX version will find the sum of the ''shortest greatest continuous subsequence''. |
||
<lang rexx>/*REXX program finds the shortest greatest continuous subsequence sum. |
<lang rexx>/*REXX program finds and displays the shortest greatest continuous subsequence sum.*/ |
||
parse arg @; w=words(@) /*get arg list; number words in list. */ |
parse arg @; w=words(@) /*get arg list; number words in list. */ |
||
say 'words='w " list="@ /*show number words & LIST to terminal |
say 'words='w " list="@ /*show number words & LIST to terminal.*/ |
||
sum=0; |
sum=0; at=w+1 /*default sum, length, and "starts at".*/ |
||
/* [↓] process the list of numbers. */ |
L=0 /* [↓] process the list of numbers. */ |
||
do j=1 for w; f=word(@,j) /*select one number at a time from list*/ |
do j=1 for w; f=word(@, j) /*select one number at a time from list*/ |
||
do k=j to w; s=f /* [↓] process a sub─list of numbers. */ |
do k=j to w; s=f /* [↓] process a sub─list of numbers. */ |
||
do m=j+1 to k; s=s+word(@,m); |
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 /*k*/ /* [↑] chose greatest sum of numbers. */ |
end /*k*/ /* [↑] chose greatest sum of numbers. */ |
||
end /*j*/ |
end /*j*/ |
||
say |
|||
$=subword(@,at,L); if $=='' then $="[NULL]" |
$=subword(@,at,L); if $=='' then $="[NULL]" /*Englishize the null (value). */ |
||
say 'sum='sum/1 " sequence="$ /*stick a fork in it, we're all done. */</lang> |
|||
'''output''' when the following was used for the list: <tt> -1 -2 3 5 6 -2 -1 4 -4 2 -1 </tt> |
'''output''' when the following was used for the list: <tt> -1 -2 3 5 6 -2 -1 4 -4 2 -1 </tt> |
||
<pre> |
<pre> |
||
Line 2,505: | Line 2,505: | ||
===longest greatest subsequential sum=== |
===longest greatest subsequential sum=== |
||
This REXX version will find the sum of the ''longest greatest continuous subsequence''. |
This REXX version will find the sum of the ''longest greatest continuous subsequence''. |
||
<lang rexx>/*REXX program finds the longest greatest continuous subsequence sum. |
<lang rexx>/*REXX program finds and displays the longest greatest continuous subsequence sum. */ |
||
parse arg @; w=words(@) /*get arg list; number words in list. */ |
parse arg @; w=words(@) /*get arg list; number words in list. */ |
||
say 'words='w " list="@ /*show number words & LIST to terminal,*/ |
say 'words='w " list="@ /*show number words & LIST to terminal,*/ |
||
sum=0; |
sum=0; at=w+1 /*default sum, length, and "starts at".*/ |
||
/* [↓] process the list of numbers. */ |
L=0 /* [↓] process the list of numbers. */ |
||
do j=1 for w; f=word(@,j) /*select one number at a time from list*/ |
do j=1 for w; f=word(@,j) /*select one number at a time from list*/ |
||
do k=j to w; _=k-j+1; s=f /* [↓] process a sub─list of numbers. */ |
do k=j to w; _=k-j+1; s=f /* [↓] process a sub─list of numbers. */ |
||
do m=j+1 to k; s=s+word(@,m); |
do m=j+1 to k; s=s+word(@, m); end /*m*/ |
||
if (s==sum & _>L) |
if (s==sum & _>L) | s>sum then do; sum=s; at=j; L=_; end |
||
end /*k*/ /* [↑] chose the longest greatest sum.*/ |
end /*k*/ /* [↑] chose the longest greatest sum.*/ |
||
end /*j*/ |
end /*j*/ |
||
say |
|||
$=subword(@,at,L); if $=='' then $="[NULL]" |
$=subword(@,at,L); if $=='' then $="[NULL]" /*Englishize the null (value). */ |
||
say 'sum='sum/1 " sequence="$ /*stick a fork in it, we're all done. */</lang> |
|||
'''output''' when the following was used for the list: <tt> 1 2 3 4 -777 1 2 3 4 0 0 </tt> |
'''output''' when the following was used for the list: <tt> 1 2 3 4 -777 1 2 3 4 0 0 </tt> |
||
<pre> |
<pre> |