简体   繁体   中英

Find the initial position of the lowest/longest sequence in an array

Similar to sleep cycles alarms, I need to cut my array in the best place possible (low numbers in my scenario) respecting a range of min/max amount of values...

To simplify, if I am able to find the longest lowest sequence in an array, I think I can move forward.

For example:

[1,2,3,0,1,4,5,6,6,0.1,1.1,2,4]

Should return 9, because 0.1 is the first value of the longest lowest, even though I have a lower value than 0.1;

[4,5,7,10,0.13,0.2,0.12,8,9,28,0.1,0.11,0.102]

Should return 10, because it is lower than 1, even though, the sequence has the same amount of numbers...

Longer sequences (in my scenario) are more important than lower. Any idea how to start this? I don't have a threshold, but a solution involving this should be ok I think (if calculated on-the-fly)

I'm not sure about your convoluted logic behind "longest lowest", but this could be a good start:

data = [4,5,7,10,0.13,0.2,0.12,8,9,28,0.1,0.11,0.102]

result = [[0,0]]
threshold = 1.0
for num, val in enumerate(data) :
    if val < threshold :
        if result[-1][1] == 0 :   # start a new sequence
            result[-1][0] = num
            result[-1][1] = 1
        else :                    # continue existing sequence
            result[-1][1] += 1
    else :    # end the previous sequence
        if result[-1][1] > 0 :
            result.append([0,0])

returns (first element, sequence length) pairs:

[[4, 3], [10, 3]]

that you may further analyse for the length, value of the first element or whatever you like.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM