mardi 20 décembre 2016

Iterating over list with IF statement (Project euler)

k = numbers

lst = []

for i in k :
    for p in range(14):
        if k[i+p] + 1 == k[i+(p+1)] or k[i+p] - 1 == k[i+(p+1)] or k[i+p] == k[i+(p+1)]:
        lst.append(k[i])
        lst.append(k[i+p])

print lst

In this code k is just a list with single integers. What I want is that k[i] is only appended to lst if the IF statement passes for p = 1 to 13.

I tried to replace the if statement with: all(k[i+p] + 1 == k[i+(p+1)] or k[i+p] - 1 == k[i+(p+1)] or k[i+p] == k[i+(p+1)])

This didn't work.

To be more clear i want to pass the thirteen adjacent digits, so for instant if 9,9,8,9,9,8,7,8,9,8,7,6,5 is in kit should append all of these numbers to lst

Aucun commentaire:

Enregistrer un commentaire