Python Exercise: Check the numbers that are higher than the previous
Count Larger Than Previous
A Python list contains some positive integers. Write a Python program to count the numbers that are greater than the previous number on the list.
Sample Data:
([1, 4, 7, 9, 11, 5]) -> 4
([1, 3, 3, 2, 2]) -> 1
([4, 3, 2, 1]) -> 0
Sample Solution-1:
Python Code:
# Function to count numbers in a list that are greater than the previous number.
def test(nums):
# Counter variable to keep track of the count.
ctr = 0
# Iterate through the list starting from the second element.
for i in range(1, len(nums)):
# Check if the current number is greater than the previous number.
if nums[i] > nums[i - 1]:
# Increment the counter.
ctr += 1
# Return the final count.
return ctr
# Example usage of the function with different lists of numbers.
nums = [1, 4, 7, 9, 11, 5]
print("Original list of numbers:", nums)
print("Count the numbers of the said list that are greater than the previous number!")
print(test(nums))
nums = [1, 3, 3, 2, 2]
print("\nOriginal list of numbers:", nums)
print("Count the numbers of the said list that are greater than the previous number!")
print(test(nums))
nums = [4, 3, 2, 1]
print("\nOriginal list of numbers:", nums)
print("Count the numbers of the said list that are greater than the previous number!")
print(test(nums))
Sample Output:
Original list of numbers: [1, 4, 7, 9, 11, 5] Count the numbers of the said list that are greater than the previous number! 4 Original list of numbers: [1, 3, 3, 2, 2] Count the numbers of the said list that are greater than the previous number! 1 Original list of numbers: [4, 3, 2, 1] Count the numbers of the said list that are greater than the previous number! 0
Explanation:
Here is a breakdown of the above Python code:
- Function definition:
- def test(nums):: Defines a function named "test()" that takes a list of numbers (nums) as input.
- Counter variable:
- ctr = 0: Initializes a counter variable to keep track of the count.
- Iteration through the list:
- for i in range(1, len(nums)):: Iterates through the list starting from the second element.
- Comparison and counting:
- if nums[i] > nums[i - 1]:: Checks if the current number is greater than the previous number.
- ctr += 1: Increments the counter if the condition is met.
- Return statement:
- return ctr: Returns the final count of numbers that are greater than the previous number.
Flowchart:
Sample Solution-2:
Python Code:
# Function to count numbers in a list that are greater than the previous number.
def test(nums):
# Use list comprehension to count numbers that are greater than the previous number.
# The expression `nums[n] > nums[n-1]` evaluates to True if the current number is greater than the previous number.
# The sum function counts the number of True values in the list comprehension.
return sum(nums[n] > nums[n-1] for n in range(1, len(nums)))
# Example usage of the function with different lists of numbers.
nums = [1, 4, 7, 9, 11, 5]
print("Original list of numbers:", nums)
print("Count the numbers of the said list that are greater than the previous number!")
print(test(nums))
nums = [1, 3, 3, 2, 2]
print("\nOriginal list of numbers:", nums)
print("Count the numbers of the said list that are greater than the previous number!")
print(test(nums))
nums = [4, 3, 2, 1]
print("\nOriginal list of numbers:", nums)
print("Count the numbers of the said list that are greater than the previous number!")
print(test(nums))
Sample Output:
Original list of numbers: [1, 4, 7, 9, 11, 5] Count the numbers of the said list that are greater than the previous number! 4 Original list of numbers: [1, 3, 3, 2, 2] Count the numbers of the said list that are greater than the previous number! 1 Original list of numbers: [4, 3, 2, 1] Count the numbers of the said list that are greater than the previous number! 0
Explanation:
Here is a breakdown of the above Python code:
- Function definition:
- def test(nums):: Defines a function named "test()" that takes a list of numbers (nums) as input.
- List comprehension and sum:
- return sum(nums[n] > nums[n-1] for n in range(1, len(nums))): Uses list comprehension to create a list of True/False values based on whether each number is greater than the previous one. The "sum()" function then counts the number of 'True' values.
Flowchart:
Python Code Editor:
Have another way to solve this solution? Contribute your code (and comments) through Disqus.
Previous: Sum of the digits in each number in a list is equal
Next: N x N square consisting only of the integer N.
What is the difficulty level of this exercise?
Test your Programming skills with w3resource's quiz.
- Weekly Trends and Language Statistics
- Weekly Trends and Language Statistics