Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@shashisp
Created September 5, 2015 08:45
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save shashisp/4279be638d797d17fb5b to your computer and use it in GitHub Desktop.
Save shashisp/4279be638d797d17fb5b to your computer and use it in GitHub Desktop.
s = raw_input().split(',')
s = map(int, s)
max_sum = 0
max_so_far = 0
left_i = 0
right_i = 0
#check all elements in the given list are positive
def all_positive(s):
return filter(lambda x: x > 0, s) == s
#check all elements in the given list are negative
def all_negative(s):
return filter(lambda x: x < 0, s) == s
if all_negative(s):
print max(s)
elif all_positive(s):
print ','.join(map(str, s))
else:
for i in range(1, len(s)):
if max_sum < 0:
left_i = i
max_sum = s[i]
else:
max_sum += s[i]
if (max_sum> max_so_far):
right_i = i
max_so_far = max_sum;
s = s[left_i:right_i+1]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment