Python Program To Check Whether The Given List Is Valley Or Not

Any sequence of integers consisting of strictly decreasing values followed by strictly increasing values such that the decreasing and increasing sequence have a minimum length of 2 and the last value of decreasing sequence is the first value of the increasing sequence is said to be a Valley.

The sequence [ 4, 3. 2, 1, 2, 3, 4] is an example of a Valley. In this article, we will create a Python Program to check wheater the given sequence is a valley or not.

Python Program To Check Whether The Given List Is Valley Or Not


def valley(l):
    if (len(l) < 3):
        return False

    up_count = 1
    low_count = 1

    for i in range(0, len(l) - 1):
        if l[i] > l[i + 1]:
            if low_count > 1:
                return False
            up_count = up_count + 1
        if l[i] < l[i + 1]:
            low_count = low_count + 1
        if l[i] == l[i + 1]:
            return False
            
    if up_count > 1 and low_count > 1:
        return True
    else:
        return False

print(valley([3, 2, 8, 1, 2, 3]))

print(valley([3, 2, 1, 2, 3]))


Output:

False 
True

Related Articles

Leave a Reply

Your email address will not be published. Required fields are marked *

Close