samedi 20 août 2016

Bypassing conditions

Can anybody understand why the following code fails?

def main(A):
    A.sort()
    B = A[:]
    ll = len(B)
    while ll > 1:
        for i in range(ll):
            for n in range(i + 1, ll):
                 if (B[i] + B[n]) % 2 == 0:
                    B.remove(B[n])
                    B.remove(B[i])
                    main(B)
    return B
if __name__ == '__main__':
    result = main([4, 5, 3, 7, 2])
    print(result)

It runs ok until my list has only one value, reaches the "return B" statement, and then it jumps back into the loop again. What am I missing???

Aucun commentaire:

Enregistrer un commentaire