dimanche 30 septembre 2018

Why does this index give me an error when the same line works if I change the condition?

Given a sequence of integers as an array, determine whether it is possible to obtain a strictly increasing sequence by removing no more than one element from the array. This is the problem I am trying to solve.

If I use this code, it gives me an index error. However, if I keep everything the same and change the condition del sequence[i] to something else, it doesn't give me the error. However, I want to delete any numbers in the list that are less than the numbers before them and then see whether the length of the new list differs from the old one by more than 1. I don't know where I went wrong.

def almostIncreasingSequence(sequence):
    originalSequence = sequence
    for i in range(1,len(sequence)):
        if sequence[i] <= sequence[i-1]:
            del sequence[i]

    if len(originalSequence) > len(sequence) - 1:
        return False
    else:
        return True

Aucun commentaire:

Enregistrer un commentaire