Search code examples
pseudocode

Counting how many "countdown" sequences exists in array


I know this sounds kind of simple but I'm trying to get the count of "countdown" sequences that exists in my array. Example: [1,2,3,2,5,4,3,0] - > 2 ([3,2] and [5,4,3]) I just need a little push, please!


Solution

  • Just iterate through the list every time the countdown breaks you increment the counter

    Python, also known as pseudo code:

    def count_finder(l):
        prev = l[0]
        counter = 0
        inCount = False
    
        for num in l[1:]:
            if num == prev-1:    #Checks if the previous was 1 greater than this one
                inCount = True   #       if it is then "inCount" is True
    
            elif num+1 != prev and inCount:  #Checks if your exiting a countdown
                inCount = False              
                counter += 1   #Increment Counter
            prev = num         #Change previous number to current number for next loop
            
        if inCount: counter+=1  #If the loop ends while in a count down increment counter
        return counter
        
    print(count_finder([9, 8, 7, 6, 5, 4]))