mardi 27 février 2018

list index out of range only when len(list) is larger than 3

So here's piece of code I wrote. now this code works for any list whose length is less than 4 but whenever I try to call it for a list that has more than 3 elements it gives me the "list index out of range error" plus it never goes into the if condition.. any explanation would be helpful

counter = 0
def almostIncreasingSequence(sequence):
    for i in range(len(sequence) - 1):
        global counter
        sequence_removed = sequence.remove(sequence[i])
        sorted_sequence = sorted(sequence)
        if sequence_removed == sorted_sequence:
            counter += 1
    print counter
    if counter > 1:
        return False
    else:
        return True


print almostIncreasingSequence([1,2,3,4])

Aucun commentaire:

Enregistrer un commentaire