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 get_maxsum(numbers):
res = numbers[0]
size = len(numbers)
for i in range(size):
for j in range(i,size):
if sum(numbers[i:j]) > res:
res = sum(numbers[i:j])
return res
# manually entering a list for the test
test_list = []
size = int(input("Enter the length of the list "))
for i in range(size):
test_list.append(int(input("Enter element list ")))
print(" largest sum",get_maxsum(test_list))
Comments
Leave a comment