Greatest subsequential sum: Difference between revisions

Content added Content deleted
(add RPL)
Line 3,356: Line 3,356:
[-1, -2, -3, -4, -5] -> []
[-1, -2, -3, -4, -5] -> []
[] - > 0
[] - > 0
</pre>

=={{header|RPL}}==
{{works with|HP|48G}}
≪ DUP SIZE -> input size
≪ { }
'''CASE'''
size NOT '''THEN END''' <span style="color:grey">@ empty list case</span>
size 1 == '''THEN''' <span style="color:grey">@ singleton case</span>
'''IF''' in 1 GET 0 < '''THEN''' SWAP '''ELSE''' input '''END'''
'''END'''
input 0 < ΠLIST NOT '''THEN''' <span style="color:grey">@ for any list with at least 1 item > 0</span>
input ≪ MAX ≫ STREAM <span style="color:grey">@ initialize sum with maximum item</span>
+ LASTARG SWAP DROP
1 size 2 - '''FOR''' len
1 size len - '''FOR''' j
input j DUP len + SUB
DUP ∑LIST 3 PICK
'''IF''' OVER < '''THEN''' 4 ROLL 4 ROLL '''END'''
DROP2
'''NEXT NEXT'''
DROP
'''END'''
'''END'''
≫ ≫ '<span style="color:blue">BIGSUB</span>' STO
{ { -1 } { -1 2 -1 } { -1 2 -1 3 -1 } { -1 1 2 -5 -6 } { -1 -2 3 5 6 -2 -1 4 -4 2 -1 } }
1 ≪ <span style="color:blue">BIGSUB</span> ≫ DOLIST
{{out}}
<pre>
1: { { } { 2 } { 2 -1 3 } { 1 2 } { 3 5 6 -2 -1 4 } }
</pre>
</pre>