Anonymous user
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
<lang rexx>/*REXX program finds the shortest greatest
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
if s>sum then do; sum=s; at=j;
end
end /*j*/▼
say;
{{out}} when the following was used for the list: <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: <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: <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 sum of the ''longest greatest
<lang rexx>/*REXX program finds the longest greatest
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
do j=1 for w; f=word(@,j) /*select one number at a time. */
if (s==sum & _>L) | s>sum then do; sum=s; at=j;
say;
{{out}} when the following was used for the list: <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: <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
|