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
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)
Comments
Leave a comment