Created
October 11, 2013 19:15
-
-
Save swapi/6940450 to your computer and use it in GitHub Desktop.
Max Consecutive Sum
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def large_sum(el): | |
class _helper: | |
large_pos = len(el) | |
current_pos = len(el) | |
large = [] | |
current = [] | |
h = _helper() | |
def _large_sum(_el, pos): | |
if pos == len(_el) - 1: | |
h.large_pos -= 1 | |
h.current_pos -= 1 | |
if _el[pos] < 0: | |
return 0 | |
else: | |
h.current = h.large = [_el[pos]] | |
return _el[pos] | |
csum = _large_sum(_el, pos + 1) + _el[pos] | |
if csum > 0: | |
if csum > sum(h.large): | |
h.large = h.current = [_el[pos]] + h.current | |
h.current_pos -= 1 | |
h.large_pos = h.current_pos | |
else: | |
h.current = [_el[pos]] + h.current | |
h.current_pos -= 1 | |
return csum | |
else: | |
h.current = [] | |
h.current_pos -= 1 | |
return 0 | |
_large_sum(el, 0) | |
print h.large | |
large_sum([-1, 5, 6, -2, 20, -50, 4]) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment