Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
Python program to find the Decreasing point in a List
When it is required to find the decreasing point in a list, a simple iteration and the break statement are used. The decreasing point is the first index where an element is greater than its next element.
Example
Below is a demonstration of the same ?
my_list = [21, 62, 53, 94, 55, 66, 18, 1, 0]
print("The list is :")
print(my_list)
my_result = -1
for index in range(0, len(my_list) - 1):
if my_list[index + 1] < my_list[index]:
my_result = index
break
print("The result is :")
print(my_result)
Output
The list is : [21, 62, 53, 94, 55, 66, 18, 1, 0] The result is : 1
How It Works
Explanation
A list of integers is defined and displayed on the console.
A variable
my_resultis initialized to -1 (indicates no decreasing point found).The list is iterated from index 0 to length-2 to avoid index out of bounds.
For each index, we compare the current element with the next element.
If the next element is smaller than the current element, we found the decreasing point.
The index where the decrease starts is stored and the loop breaks.
In this example, at index 1, element 62 is greater than element 53 at index 2, so the result is 1.
Conclusion
This program efficiently finds the first decreasing point in a list by comparing consecutive elements. The algorithm returns -1 if no decreasing point exists (i.e., the list is non-decreasing).
