Answer to Question #206076 in Python for Sudheer

Question #206076

def max_sub_list(l:list):

if len(l) == 0:

print(0)

else:

max_sum = l[0]

for i in range(len(l)):

for j in range(i, len(l)):

if sum(l[j-i:j+1]) > max_sum:

max_sum = sum(l[j-i:j+1])

print(max_sum)

while True:

try:

arr = list(map(int,input().split()))

except ValueError:

continue

max_sub_list(arr)

Output:6


Traceback (most recent call last):

File "main.py", line 13, in <module>

arr = list(map(int,input().split()))

EOFError: EOF when reading a line

Can anyone give correct code



1
Expert's answer
2021-06-11T14:11:31-0400

your code works fine if you are using python 3

problem in your IDE. try running your code in another IDE. or use the corrected code below

def max_sub_list(l:list):
	if len(l) == 0:
		print(0)
	else:
		max_sum = l[0]
		for i in range(len(l)):
			for j in range(i, len(l)):
				if sum(l[j-i:j+1]) > max_sum:
					max_sum = sum(l[j-i:j+1])
		print(max_sum)
while True:
	try:
		arr = list(map(int,input().split()))
	except (ValueError, EOFError):
		continue
	max_sub_list(arr)

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