0

I've seen some posts about this same question, and I think my logic is pretty much the same as their answers. But I cannot find where exactly I'm wrong here.

  • My code first checks the length of the provided sequence, if it is 2 or less it automatically returns True.
  • Next, it removes(pops) the first element and check if the rest are in ascending order.
  • If the sequence isn't in order, it replaces it with the original sequence and repeats the second step, but this time it removes the next element (pop(i)).
  • This continues until there are no more elements to remove, which ultimately returns as False
  • If in any of the iterations, the list is found to be in ascending order, the function returns True.

This is the code:

def almostIncreasingSequence(sequence):
    original = sequence.copy()
    if len(sequence) <= 2: return True
    for i in range(len(sequence)):
        sequence.pop(i)
        # print(sequence)
        for j in range(len(sequence)-1):
            if sequence[j+1] <= sequence[j]:
                sequence = original.copy()
            elif j+1 == len(sequence)-1:
                return True
        if i == len(sequence)-1:
            return False
    

And this is my result :'(

enter image description here

I think my logic may not be correctly implemented in the code. But I don't know how to test it. It'd be helpful if you can give me a sequence where this function will give a wrong answer.


Solve almostIncreasingSequence (Codefights)

This is one of the posts I was referring to at the very beginning. It also explains the almostIncreasingSequence(sequence) question and the answer explains the logic behind the code.

ray_lv
  • 99
  • 5
  • 1
    That response isn't telling you that you're generating the wrong answer, it's telling you that it's too slow. - try a sequence like [1,2,3 ... 9999, 10000, 9999, 9999] to start to get a feeling for how slow. This problem on 10,000 elements should still be essentially instantaneous (<0.1s) on any modern hardware. – moreON Mar 24 '21 at 05:36
  • Yes, It didn't occur to me to read the error message in full! I guess my approach is slow because I'm copying lists at each iteration. Can you give a suggestion as to how can I make it faster. – ray_lv Mar 24 '21 at 05:58
  • Iterate through the sequence no more than one time. – moreON Mar 24 '21 at 06:07

1 Answers1

0

You don't have to try every element. Just find the violation of the ascension, and try to resolve it by removing one of the violators. Then check the rest of the list.

More formally, suppose that the sequence[:i] is in the ascending order, but sequence[i] < sequence[i+1]. You cannot keep them both; one must be gone. Which one, depends on sequence[i-1].

If sequence[i+1] < sequence[i-1], removal of sequence[i] wouldn't help: a violation will remain. Therefore, remove sequence[i+1]. Otherwise, remove sequence[i] (do you see why?). Finally, check that the rest of sequence is ascending.

user58697
  • 7,808
  • 1
  • 14
  • 28