Enable Javascript in your browser and then refresh this page, for a much enhanced experience.
First solution in Clear category for Stair Steps by kamilinho20
def checkio(numbers):
result = []
i = 0
while i < len(numbers):
first = numbers[i]
try:
second = numbers[i + 1]
except:
second = 0
if first > 0 and second > 0:
result.extend([first, second])
i += 1
elif first > 0:
result.append(first)
elif second > 0:
result.append(second)
i += 1
else:
try:
third = numbers[i + 2]
except:
third = 0
if third >= 0:
if first > second:
result.append(first)
else:
result.append(second)
i += 1
else:
if (first + third) > second:
result.append(first)
else:
result.append(second)
i += 1
i += 1
print(first, second, i)
print(result, sum(result))
return sum(result)
#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. 12, 2017