Enable Javascript in your browser and then refresh this page, for a much enhanced experience.
Dijkstra hum ??? solution in Clear category for Stair Steps by rybld2
def edsger(l):
"""l[0] et l[len(l) - 1] doivent être nuls pour les palliers"""
if not l[0] == 0: l = [0] + l
if not l[len(l) - 1] == 0: l = l + [0, 0]
lignes = [-float('inf') for x in range(len(l))]
lignes[0] = 0
i, choix = 0, 0
while i < len(l) - 2:
lignes[i + 1] = max(l[i + 1] + choix, lignes[i + 1])
lignes[i + 2] = max(lignes[i + 2], l[i + 2] + choix)
choix = max(lignes[i + 1], lignes[i + 2])
i = i + 1 if choix == lignes[i + 1] else i + 2
return lignes[len(l) - 2]
def separer(nb):
""" renvoie nb en listes positive negatives distinctes
ex : separer([-21, 23, -69, -67]) renvoie [[-21], [23], [-69, -67]]"""
res = []
i = 0
while i < len(nb):
ss_l = []
j = i
while (j < len(nb) - 1) and ((nb[j] >= 0 and nb[j + 1] >= 0) or (nb[j] < 0 and nb[j + 1] < 0)):
ss_l.append(nb[j])
j += 1
ss_l.append(nb[j])
res.append(ss_l)
i = j + 1
return res
def checkio(numbers):
res = 0
sep = separer(numbers)
for x in sep:
plus=sum(x) if x[0]>=0 else edsger(x)
res += plus
return res
#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')
March 24, 2021
Comments: