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

Django 4.1 adds async-compatible interface to QuerySet

The much-awaited pull request for an async-compatible interface to Queryset just got merged into the main branch of Django.Pull Request - https://github.com/django/django/pull/14843 The Django core team has been progressively adding async suppor…
Read more →

3 min read

Making Django Admin Jazzy With django-jazzmin

Django admin is undoubtedly one of the most useful apps of Django. Over the years there has been very little change in the admin app as far as the UX is concerned and it's not a bad thing at all. Django admin was designed to provide a simple and minimali…
Read more →

4 min read