Python Program that prints the count of either peaks or valleys from a list
Last Updated :
08 Mar, 2023
Given a List, the following article shows ways to count elements which are either peak(x > y < z) or valley(x < y > z).
Input : test_list = [1, 2, 4, 2, 6, 7, 8, 3]
Output : 3
Explanation : (2, 4, 2), (4, 2, 6), (7, 8, 3) are peaks and valleys.
Input : test_list = [1, 2, 4, 5, 3, 2]
Output : 1
Explanation : (4, 5, 3) is peak.
Method 1 : Using loop
In this, we iterate for each element and check if its next and previous element is either smaller or larger than the current element. If found, the counter is incremented.
Python3
# initializing list
test_list = [1, 2, 4, 2, 6, 7, 8, 3]
# printing original list
print("The original list is : " + str(test_list))
res = 0
for idx in range(1, len(test_list) - 1):
if test_list[idx + 1] > test_list[idx] < test_list[idx - 1] or test_list[idx + 1] < test_list[idx] > test_list[idx - 1]:
res += 1
# printing result
print("Peaks and Valleys count : " + str(res))
Output:
The original list is : [1, 2, 4, 2, 6, 7, 8, 3]
Peaks and Valleys count : 3
Time Complexity: O(n)
Auxiliary Space: O(n)
Method 2 : Using list comprehension and len()
In this, we perform the task of getting peak and valleys using list comprehension and then len() is used to compute the exact count.
Python3
# initializing list
test_list = [1, 2, 4, 2, 6, 7, 8, 3]
# printing original list
print("The original list is : " + str(test_list))
# one-liner alternative
res = len([test_list[idx] for idx in range(1, len(test_list) - 1) if test_list[idx + 1] >
test_list[idx] < test_list[idx - 1] or test_list[idx + 1] < test_list[idx] > test_list[idx - 1]])
# printing result
print("Peaks and Valleys count : " + str(res))
Output:
The original list is : [1, 2, 4, 2, 6, 7, 8, 3]
Peaks and Valleys count : 3
Time Complexity: O(n), where n is the length of the input list. This is because we’re using the list comprehension and len() which has a time complexity of O(n) in the worst case.
Auxiliary Space: O(1), as we’re not using any additional space other than the input list itself.
Similar Reads
Python program to Count the Number of occurrences of a key-value pair in a text file Given a text file of key-value pairs. The task is to count the number of occurrences of the key-value pairs in the file with PythonProgram to Count the occurrences of a key-value pairNaive Approach to Count the Occurrences of a key-value PairUsing Python built-in collections.CounterUsing regular exp
3 min read
Python program to unique keys count for Value in Tuple List Given dual tuples, get a count of unique keys for each value present in the tuple. Input : test_list = [(3, 4), (1, 2), (2, 4), (8, 2), (7, 2), (8, 1), (9, 1), (8, 4), (10, 4)] Output : {4: 4, 2: 3, 1: 2} Explanation : 3, 2, 8 and 10 are keys for value 4. Input : test_list = [(3, 4), (1, 2), (8, 1),
6 min read
Python | Program to count number of lists in a list of lists Given a list of lists, write a Python program to count the number of lists contained within the list of lists. Examples: Input : [[1, 2, 3], [4, 5], [6, 7, 8, 9]] Output : 3 Input : [[1], ['Bob'], ['Delhi'], ['x', 'y']] Output : 4 Method #1 : Using len() Python3 # Python3 program to Count number #
5 min read
Counting number of unique values in a Python list Counting the number of unique values in a Python list involves determining how many distinct elements are present disregarding duplicates.Using a SetUsing a set to count the number of unique values in a Python list leverages the property of sets where each element is stored only once.Pythonli = [1,
2 min read
Python - Count if dictionary position equals key or value Given a dictionary, count instances where dictionary item position equals key or value. Valid for Py >= 3.6 [ Introduction of dictionary ordering ]. Input : test_dict = {5:3, 2:3, 10:4, 7:3, 8:1, 9:5} Output : 2 Explanation : At 3 and 5th position, values are 3 and 5. Input : test_dict = {5:3, 2:
3 min read
Python program to count Bidirectional Tuple Pairs Given Tuple list, compute bidirectional tuples. Input : test_list = [(5, 6), (1, 2), (6, 5), (9, 1), (6, 5), (2, 1)] Output : 3 Explanation : (1, 2), (2, 1); (5, 6) -> [(6, 5), (6, 5)], total 3 pairs. Input : test_list = [(5, 6), (1, 3), (6, 5), (9, 1), (6, 5), (2, 1)] Output : 2 Explanation : (5
4 min read