Enable Javascript in your browser and then refresh this page, for a much enhanced experience.
First solution in Uncategorized category for Stair Steps by Mahoter
def checkio(numbers):
res = 0
a = 0
while a < len(numbers):
if numbers[a] < 0 and a == len(numbers)-1:
a+=2
elif numbers[a] < 0 and numbers[a+1] > 0:
res += numbers[a+1]
a += 2
elif numbers[a] > 0:
res += numbers[a]
a+= 1
elif numbers[a]<0 and numbers[a+1]< 0 :
if a+2 == len(numbers):
if numbers[a] > numbers[a+1]:
res += numbers[a]
a +=1
else:
res += numbers[a+1]
a += 2
else:
if a+3 < len(numbers):
if numbers[a] + numbers[a+2] > numbers[a+1] + numbers[a+3]:
res += numbers[a]
a += 1
else:
res += numbers[a+1]
a += 2
elif numbers[a+2] > 0:
if numbers[a] > numbers[a+1]:
res += numbers[a]
a +=1
else:
res += numbers[a+1]
a += 2
elif numbers[a] + numbers[a+2] > numbers[a+1]:
res += numbers[a]
a += 1
else:
res += numbers[a+1]
a += 2
else:
a += 1
return res
#These "asserts" using only for self-checking and not necessary for auto-testing
if __name__ == '__main__':
assert checkio([-11,69,77,-51,23,67,35,27,-25,95]) == 393, '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, 2016