- Data Structure
- Networking
- RDBMS
- Operating System
- Java
- MS Excel
- iOS
- HTML
- CSS
- Android
- Python
- C Programming
- C++
- C#
- MongoDB
- MySQL
- Javascript
- PHP
- Physics
- Chemistry
- Biology
- Mathematics
- English
- Economics
- Psychology
- Social Studies
- Fashion Studies
- Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
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 −
Example
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]))
Input
[10, 12, 23, 34, 55]
Output
True
Advertisements