Almost increasing sequence in python

increasing decreasing sequence in python
python increasing list
python check if numbers are strictly increasing
increasing sequence python
codefights python

The goal of the code is to see if a sequence is almost increasing, that is, it can be made strictly increasing by removing a single element.

For example: [1, 3, 2, 3] would be strictly increasing if the element at index 1 were removed. [1, 2, 1, 2] is not almost increasing because if you removed the first '2', you would get [1, 1, 2] which is not strictly increasing.

My code has to work in under 4000 ms for a sequence of length 2 <= len <= 10^5. It is likely getting caught up on very long sequences.

def almostIncreasingSequence(sequence):
    length = len(sequence)
    count = 0

    for previous, next in zip(sequence[:-1],sequence[1:]):
        if previous < next:
            count +=1

    if (length-count>2):
        return False
    return True

[1, 2, 1, 2]

[1, 2, 3, 4, 5, 3, 5, 6]

[40, 50, 60, 10, 20, 30]

would not work.. any help please

Please note that this is not a homework question, as most of you guys know, there are lots of answers to this question posted online and i was wondering if i can do it this way


This works:

import timeit
import random


def increasing_sequence_pos(sequence):
    for n, (a, b) in enumerate(zip(sequence[:-1], sequence[1:])):
        if a >= b:
            return False, n + 1
    return True, -1


def almost_increasing_sequence(sequence):
    increasing, n = increasing_sequence_pos(sequence)
    return (
        # either it was already increasing
        increasing or
        # or the problem is with the last element
        n == len(sequence)-1 or
        ((
            # or the element at position n was the problem
            (sequence[n - 1] < sequence[n + 1]) or
            # or the element at position n-1 was the problem
            (sequence[n - 2] < sequence[n] < sequence[n + 1])
        ) and increasing_sequence_pos(sequence[n + 1:])[0])
    )


size = 1000000

# time on simple increasing series
numbers = list(range(size))
print(timeit.timeit(lambda: almost_increasing_sequence(numbers), number=1))
print(f'Result: {almost_increasing_sequence(numbers)}')

# time on single issue
numbers[random.randint(1, size)] = 0
print(timeit.timeit(lambda: almost_increasing_sequence(numbers), number=1))
print(f'Result: {almost_increasing_sequence(numbers)}')

# time on two issues issue
numbers[random.randint(1, size)] = 0
print(timeit.timeit(lambda: almost_increasing_sequence(numbers), number=1))
print(f'Result: {almost_increasing_sequence(numbers)}')

Note how you get varying times and the time of course depends on the hardware you're executing on, so "4000ms" is rather silly (and very easy to achieve on modern computers, even with really, really bad code).

You'll notice that the first is fairly steady (0.07 seconds on my workstation), while the second and third typically take a lot shorter to just a bit shorter.

Example output:

0.07000060000000001
Result: True
0.06909959999999998
Result: True
0.06430069999999999
Result: False

Note that an increasing sequence is also considered an almost increasing sequence.

Solve almostIncreasingSequence (Codefights), Here is Python code that passes all the tests you show. j = first_bad_pair(​sequence) if j == -1: return True # List is increasing if first_bad_pair(sequence[j-1:​j] +  There is no one element in this array that can be removed in order to get a strictly increasing sequence. For sequence = [1, 3, 2], the output should be almostIncreasingSequence (sequence) = true. You can remove 3 from the array to get the strictly increasing sequence [1, 2].


def seqIncCheck(lst):
    temp = lst[0]
    for i in range(1,len(lst)):
        if lst[i] > temp:
           temp = lst[i]
           continue
        else:
           return False
    return True

Python, Python | Check if list is strictly increasing. The test for monotonic sequence is a utility that has manifold applications in mathematics and hence every sphere  Task Given a sequence of integers, check whether it is possible to obtain a strictly increasing sequence by erasing no more than one element from it. Example For sequence = [1, 3, 2, 1], the


Your algorithm is not even right. You are just checking if there is one or less element that is smaller that its previous. However it does not mean it is a "almost-increasing" sequence.

In brief, when you meet an element that is smaller than its previous, then sequence is "almost-increasing" if the sequence is "strictly increasing" by removing that element itself, OR by removing the previous element.

e.g.

1, 2, 3, [2*], 4, 5, 6
1, 2, 3, 100*, [4], 5, 6
1, 2, 3, [1], 2, 3

In the first 2 example, number in [] is the element that is smaller than previous. However, number with * is the number need to be removed to form a strictly-increasing sequence. 3rd example is an example that contains only 1 element that is smaller than previous, but it is not a almost-increasing sequence

So the most straight-forward logic (in pseudo-code) is something like:

def is_almost_increasing(seq):
    for i in 1 to len(seq):
        if seq[i] <= seq[i-1]:
            return (  is_strictly_increasing(seq[:i-1] + seq[i:] 
                     or is_strictly_increasing(seq[:i] + seq[i+1:] )
    return true

(is_strictly_increasing is simple, will just leave it to you)

There are some tricks to make this even faster (e.g. you don't really need to re-check the part that you have already confirmed increasing), but this should be a good starting point for you.

[Help] Codefight almostIncreasingSequence(sequence) : learnpython, 4.1: if squence (with i removed) is not an increasing sequence -> return False After 5 years of learning Python as a hobby, yesterday I did my first contribution  There is no one element in this array that can be removed in order to get a strictly increasing sequence. For sequence = [1, 3, 2], the output should be almostIncreasingSequence(sequence) = true. You can remove 3 from the array to get the strictly increasing sequence [1, 2]. Alternately, you can remove 2 to get the strictly increasing sequence [1, 3].


AlmostIncreasingSequence, AlmostIncreasingSequence | CodeFights Intro Algorithm JavaScript Solution and Breakdown Duration: 7:07 Posted: Jul 11, 2017 Introduction to Sequences in Python. In Python, Sequences are the general term for ordered sets. In these Sequences in Python article, we shall talk about each of these sequence types in detail, show how these are used in python programming and provide relevant examples.


Finding an increasing sequence from a given array in a time-efficient , I'm asking for a more performance and resource efficient implementation. I'm also an amateur in Python. share. There is no one element in this array that can be removed in order to get a strictly increasing sequence. For sequence [1, 3, 2], the output should be: almostIncreasingSequence(sequence) = true. You can remove 3 from the array to get the strictly increasing sequence [1, 2]. Alternately, you can remove 2 to get the strictly increasing sequence [1, 3].


[Solved] Almost increasing sequence, You immediate problem is. Quote: while(i<sequence.size()). You have to change it to. Hide Copy Code. while(i < (sequence.size() - 1) ) Note: Only compatible with Python 3 at the moment. Should be almost compatible with python 2. Run. Once keras-tcn is installed as a package, you can take a glimpse of what's possible to do with TCNs. Some tasks examples are available in the repository for this purpose: