How to count the number of strictly increasing subarrays in a list with Python

1 Answer

0 votes
def countStrictlyIncreasingSubarrays(lst):
    count = 0
 
    for i in range(len(lst)):
        for j in range(i + 1, len(lst)):
            if lst[j - 1] >= lst[j]:
                break
 
            count = count + 1
 
    return count
 

# Example 1  
lst = [1, 2, 3, 4]

# {1, 2}, {1, 2, 3}, {1, 2, 3, 4}, {2, 3}, {2, 3, 4}, {3, 4}
 
print("Count of strictly increasing subarrays = ", countStrictlyIncreasingSubarrays(lst))


# Exampe 2
lst = [1, 3, 4, 0, 7, 5, 9];

# {1, 3}, {1, 3, 4}, {3, 4}, {0, 7}, {5, 9}

print("Count of strictly increasing subarrays = ", countStrictlyIncreasingSubarrays(lst))



'''
run:

Count of strictly increasing subarrays =  6
Count of strictly increasing subarrays =  5

'''

 



answered Aug 21, 2022 by avibootz
...