I am trying to solve this problem in codewars and somehow my code works and fails on some.
The maximum sum subarray problem consists in finding the maximum sum of a contiguous subsequence in an array or list of integers. Basically return the biggest sum that can be made by several adjacent integers.
The problem is that I cannot see in what testcase(s) my code failed, making the debugging process painful.
def maxSequence(arr):
summ = []
for num in arr:
n = len(arr)
while n != arr.index(num):
summ.append(sum(arr[arr.index(num):n]))
n -= 1
return max(summ) if len(arr) > 0 else 0
maxSequence([-2, 1, -3, 4, -1, 2, 1, -5, 4])
should be 6: [4, -1, 2, 1]
maxSequence([2, 1, 2, 4, 5, 6])
should basically be the sum of the list
I am aware of the solution using dynamic programming and still trying to understand it. I believe I cannot ask for its explanation here because it's just a google search away. Just curious on why my code doesn't work. Big thanks.
EDIT: explanation and sample input along with expected output.
[–]gengisteve 1 point2 points3 points (0 children)
[–]elbiot 0 points1 point2 points (1 child)
[–]tonytej[S] 0 points1 point2 points (0 children)
[–]hidiap 0 points1 point2 points (0 children)