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

1 min read

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

PROGRAMS

Latest Articles

Latest from djangocentral

Capturing Query Parameters of request.get in Django

In Django, the request object contains a variety of information about the current HTTP request, including the query parameters. Query parameters are a way to pass additional information in the URL and are used to filter or sort data. The request object p…
Read more →

2 min read

Understanding related_name in Django Models

In Django, related_name is an attribute that can be used to specify the name of the reverse relation from the related model back to the model that defines the relation. It is used to specify the name of the attribute that will be used to access the relat…
Read more →

2 min read