Support Our Site

To ensure we can continue delivering content and maintaining a free platform for all users, we kindly request that you disable your adblocker. Your contribution greatly supports our site's growth and development.

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

How to Use Subquery() in Django With Practical Examples

In the realm of web development, Django stands as a powerful and versatile framework for building robust applications. One of the key aspects of developing efficient and optimized web applications is handling database queries effectively. In this article…
Read more →

4 min read

DRF Serializer: Handling OrderedDict and Converting It to a Dictionary or JSON

In Django Rest Framework (DRF) tests, when you access serializer.data, you might encounter an OrderedDict instead of a regular dictionary. This behavior is intentional and reflects the design of DRF's serialization process.Understanding the Problem The u…
Read more →

3 min read

Django Rest Framework CheetSheet: Mastering API Development

Django Rest Framework (DRF) is a powerful toolkit that makes building robust and scalable web APIs with Django a breeze. Whether you're a seasoned Django developer or a newcomer, having a comprehensive cheat sheet at your disposal can be a game-changer. …
Read more →

5 min read

How to Perform NOT Queries in Django ORM

In Django, performing NOT queries allows you to exclude certain records from the query results based on specific conditions. The NOT operator, represented by the tilde (~) when used in conjunction with the Django ORM's Q object, helps you construct compl…
Read more →

3 min read