Enable Javascript in your browser and then refresh this page, for a much enhanced experience.
First solution in Clear category for Stair Steps by wo.tomasz
def checkio(numbers):
numbers.insert(0, 0)
l = len(numbers)
numbers.extend([0, 0, 0, 0])
i = 0
counter = 0
while i < l:
counter += numbers[i]
sumlist = [(sum(numbers[i+1:i+5]), 1),
(sum(numbers[i+1:i+4]), 1),
(sum([numbers[i+1] + numbers[i+2] + numbers[i+4]]), 1),
(sum([numbers[i+1] + numbers[i+3] + numbers[i+4]]), 1),
(sum([numbers[i+1] + numbers[i+3]]), 1),
(sum(numbers[i+2:i+5]), 2),
(sum([numbers[i+2] + numbers[i+4]]), 2),
(sum(numbers[i+2:i+4]), 2)]
sumlist.sort(key= lambda x: (x[0], 2 - x[1]))
i += sumlist[-1][1]
return counter
#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')
Jan. 17, 2021
Comments: