Enable Javascript in your browser and then refresh this page, for a much enhanced experience.
First solution in Uncategorized category for Stair Steps by Kurush
# migrated from python 2.7
from collections import deque
def checkio(numbers):
global_max_numbers = float("-inf")
max_numbers = 0
search_queue = deque()
current_index = -1
search_queue.append([max_numbers, current_index]);
while True:
if not search_queue: break
max_numbers, current_index = search_queue.popleft()
if current_index > len(numbers) - 1:
if (current_index != -1) and (max_numbers > global_max_numbers):
global_max_numbers = max_numbers
continue
if current_index != -1: max_numbers += numbers[current_index]
search_queue.append([max_numbers, current_index + 1])
search_queue.append([max_numbers, current_index + 2])
return global_max_numbers
#These "asserts" using only for self-checking and not necessary for auto-testing
if __name__ == '__main__':
assert checkio([5, -3, -1, 2]) == 6, 'Fifth'
assert checkio([5, 6, -10, -7, 4]) == 8, 'First'
assert checkio([-11, 69, 77, -51, 23, 67, 35, 27, -25, 95]) == 393, 'Second'
assert checkio([-21, -23, -69, -67, 1, 41, 97, 49, 27]) == 125, 'Third'
print('All ok')
June 22, 2014