I am doing a coding exercise: 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.
So I wrote this code:
def almostIncreasingSequence(sequence):
first_list, second_list = sequence, sequence
for i in range(len(sequence)-1):
if sequence[i] >= sequence[i+1]:
first_list.remove(sequence[i])
second_list.remove(sequence[i+1])
break
if first_list == sorted(set(first_list)) or second_list == sorted(set(second_list)):
return True
else:
return False
Now this code seems to work on most sequences but this one in particular raises an error:
print almostIncreasingSequence([1,3,2])
The error is as following:
Traceback (most recent call last):
file.py3 on line ?, in getUserOutputs
userOutput = _runsppge(testInputs[i])
file.py3 on line ?, in _runsppge
return almostIncreasingSequence(*_fArgs_lujxeukjlbwc)
file.py3 on line 7, in almostIncreasingSequence
second_list.remove(sequence[i+1])
IndexError: list index out of range
I just dont understand how the list index could possibly be out of range.. Anyone got a clue?