RosettaCodeData/Task/Greatest-subsequential-sum/NetRexx/greatest-subsequential-sum....

33 lines
801 B
Plaintext

/* REXX ***************************************************************
* 10.08.2012 Walter Pachl Pascal algorithm -> Rexx -> NetRexx
**********************************************************************/
s=' -1 -2 3 5 6 -2 -1 4 -4 2 -1'
maxSum = 0
seqStart = 0
seqEnd = -1
Loop i = 1 To s.words()
seqSum = 0
Loop j = i to s.words()
seqSum = seqSum + s.word(j)
if seqSum > maxSum then Do
maxSum = seqSum
seqStart = i
seqEnd = j
end
end
end
Say 'Sequence:'
Say s
Say 'Subsequence with greatest sum: '
If seqend<seqstart Then
Say 'empty'
Else Do
ol=' '.copies(seqStart-1)
Loop i = seqStart to seqEnd
w=s.word(i)
ol=ol||w.right(3)
End
Say ol
Say 'Sum:' maxSum
End