Python: Check whether a sequence of numbers has an increasing trend or not
Python Basic - 1: Exercise-98 with Solution
Write a Python program to check whether a sequence of numbers has an increasing trend or not.
Sample Solution:
Python Code:
# Define a function named increasing_trend that takes a list of numbers (nums) as an argument.
def increasing_trend(nums):
# Check if the sorted version of the list is equal to the original list.
# If the list is sorted, it means the numbers are in increasing order.
if (sorted(nums) == nums):
# Return True if the list is in increasing order.
return True
else:
# Return False if the list is not in increasing order.
return False
# Test the function with different lists of numbers and print the results.
# Test case 1
print(increasing_trend([1,2,3,4]))
# Test case 2
print(increasing_trend([1,2,5,3,4]))
# Test case 3
print(increasing_trend([-1,-2,-3,-4]))
# Test case 4
print(increasing_trend([-4,-3,-2,-1]))
# Test case 5
print(increasing_trend([1,2,3,4,0]))
Sample Output:
True False False True False
Explanation:
Here is a breakdown of the above Python code:
- Function definition:
- The code defines a function named "increasing_trend()" that takes a list of numbers (nums) as an argument.
- Comparison:
- The function checks if the sorted version of the list is equal to the original list.
- Return Statement:
- If the list is sorted, it means the numbers are in increasing order, and the function returns 'True'. Otherwise, it returns 'False'.
- Test Cases:
- The function is tested with different lists of numbers using print(increasing_trend(...)).
Visual Presentation:
Flowchart:
Python Code Editor:
Have another way to solve this solution? Contribute your code (and comments) through Disqus.
Previous: Write a Python program to find the highest and lowest number from a given string of space separated integers.
Next: Write a Python program to find the position of the second occurrence of a given string in another given string.
What is the difficulty level of this exercise?
Test your Programming skills with w3resource's quiz.
It will be nice if you may share this link in any developer community or anywhere else, from where other developers may find this content. Thanks.
https://198.211.115.131/python-exercises/basic/python-basic-1-exercise-98.php
- Weekly Trends and Language Statistics
- Weekly Trends and Language Statistics