Program to check whether list is strictly increasing or strictly decreasing in Python

Suppose we have a list of numbers; we have to check whether the list is strictly increasing or strictly decreasing.

So, if the input is like nums = [10, 12, 23, 34, 55], then the output will be True, as all elements are distinct and each element is larger than the previous one, so this is strictly increasing.

To solve this, we will follow these steps −

  • if size of nums <= 2, then
    • return True
  • if all elements in num is not distinct, then
    • return False
  • ordered := sort the list nums
  • return true when nums is same as ordered or nums is same as ordered in reverse way, otherwise false.

Let us see the following implementation to get better understanding −


 Live Demo

class Solution:
   def solve(self, nums):
      if len(nums) <= 2:
         return True
      if len(set(nums)) != len(nums):
         return False
      ordered = sorted(nums)
      return nums == ordered or nums == ordered[::-1]
ob = Solution()
print(ob.solve([10, 12, 23, 34, 55]))


[10, 12, 23, 34, 55]



Updated on: 05-Oct-2020

1K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started