Answer to Question #174676 in Python for bhuvanesh

Question #174676

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.


1
Expert's answer
2021-03-25T06:30:26-0400
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))

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
New on Blog
APPROVED BY CLIENTS