Enable Javascript in your browser and then refresh this page, for a much enhanced experience.
bit optimized solution in Clear category for Stair Steps by Krzysztof.Sawarzynski
def calculate(data):
steps, value = data
if len(steps) == 0:
return ([], value)
if len(steps) == 1:
return ([], value + steps[0])
if len(steps) > 1:
stepsFrom2 = calculate((steps[2:], value + steps[1]))
stepsFrom1 = calculate((steps[1:], value + steps[0]))
if stepsFrom2[1] > stepsFrom1[1]:
return stepsFrom2
return stepsFrom1
def checkio(numbers):
numbers.append(0)
steps, value = calculate((numbers, 0))
return value
#These "asserts" using only for self-checking and not necessary for auto-testing
if __name__ == '__main__':
assert checkio([5, -3]) == 5, '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')
Jan. 19, 2017