Given a list of integers, write a program to identify the contiguous sub-list that has the largest sum and print the sum. Any non-empty slice of the list with step size 1 can be considered as a contiguous sub-list.
def largest_sum_contiguous_sublist(x):
"""
Parameters:
x: list of integers
Returns:
the largest sum of x sublist elements
"""
glob_max = 0
loc_max = 0
for elem in x:
loc_max += elem
if loc_max < 0:
loc_max = 0
elif glob_max < loc_max:
glob_max = loc_max
return glob_max
Comments
Leave a comment