I was trying to understand Kadane's algorithm from Wikipedia, when I found this:
def max_subarray(A):
max_ending_here = max_so_far = A[0]
for x in A[1:]:
max_ending_here = max(x, max_ending_here + x)
max_so_far = max(max_so_far, max_ending_here)
return max_so_far
I'm not familiar with Python. I tried to google what this syntax does but I couldn't find the right answer because I didn't know what's it called. But, I figured A[1:]
is the equivalent of omitting A[0]
, so I thought for x in A[1:]:
is equivalent to for(int i = 1; i < A.length; i++)
in Java
But, after changing for x in A[1:]:
to for x in range(1,len(A))
, I got the wrong result
Sorry if this is a stupid question, but I don't know where else to find the answer. Can somebody tell me what this syntax does and what is it called? Also, could you give me the equivalent of for x in A[1:]:
in Java?