Return a long sequential sequence of integers

Problem

tl; dr - the code below shows an algorithm that I want to improve by trying some other approach.

Now a long explanation.

Given a list of integers, I would like to find every sequence of consecutive integers that either coincide with the starting point of the sequence, or higher. The sequence should also be longer than the sequential sequence with a higher rank.
You are probably quite confused. Let me just illustrate what I mean.

When I have a list of integers, I can expand it to represent its ranges. Example:

2,1,1,3,3,1

becomes

-,-,-,3,3,-
2,-,-,2,2,-
1,1,1,1,1,1

, , . , .

/, .
, 0 , , . [1, 6, 5]: 1 , , 6 , 5 .
. . . [2,1,0]. , 2 . ! ! . , .
: [3,2,4]

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


, . 100% , .

[3,3,3,2,1] -> [[1,5,4],[2,4,3],[3,3,2]] 

[7,7,3,0,1,2,3] -> [[1,3,6],[2,2,6],[3,3,2],[3,1,6],[7,2,1]]

[0,0,0] -> []

[0,1,0,4,0,1,0] -> [[1,1,1],[4,1,3],[1,1,5]]



, 1. , , . , , , . , :

[2,4,4,4,1], looking at 3*4
[1,8,8], looking at 2*8
[9,9,9,8], looking at 3*9
[9,7,4], looking at 2*7 (sequence doesn't formally exist as [7,7], 
                         but would be in the results, as described above)

, , .


:

[4,4,2,1,1,3]

. , ! , , - 2... 2 . ... . ([4,2,1]) . , :

[2,2,2,1,1,3]

! 2 . , , , 3. , huzzah, , . , ([3,1,5]) , 1:

[2,2,2,1,1,1]

. [2,3,2].

[1,1,1,1,1,1]

, , . , , [1,6,5], .

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

, ... .

, . :

def ListProcessing(listL, length, freq):

    #to detect end of array and not miss out on last sequences
    listL.extend([0])

    #Iterating over all unique elements that appear in the list from top to
    #bottom, leaving out elements under or equal to _length_
    for checkNum in reversed(list(set(sorted(listL))-set(range(length)))):
        seqLen = 0
        #Iterate over list
        for index, val in enumerate(listL):
            #current element higher than checkNum?
            #Yes -> increase counter of the sequence length
            if val >= checkNum:
                seqLen += 1
            #No -> Reset seqLen. If seqLen is high enough, replace sequence with
            #      sequence of highest neighbouring elements and yield the seq.
            else:
                if seqLen > freq-1:
                    newVal = max(val, listL[index-seqLen-1])
                    listL[index-seqLen:index] = [newVal] * seqLen
                    yield(checkNum, seqLen, index-1)
                seqLen = 0

AFAIK , , .

- ?

, . , , . .
array.array, . - , .

/ !

+4
1

, . , seq = [4, 4, 2, 1, 1, 3, 2]. , , , , . [min(seq), len(seq), len(seq) - 1] [1, 7, 6].

, , min(seq), seq [4, 4, 2] [3, 2] . , .

, :

def recListProcessing(seq, threshold=0, min_len=1):
    len_seq = len(seq)
    if len_seq < min_len:
        return

    min_value = min(seq)
    if min_value > threshold:
        yield (min_value, len_seq, len_seq - 1)

    start = 0
    while start < len_seq:
        try:
            end = seq.index(min_value, start)
        except ValueError:
            end = len_seq
        sub_seq = seq[start:end]
        for item in recListProcessing(sub_seq, threshold, min_len):
            yield (item[0], item[1], item[2] + start)
        start = end + 1
+3

All Articles