-
-
Notifications
You must be signed in to change notification settings - Fork 46.1k
/
monotonic_array.py
37 lines (32 loc) · 946 Bytes
/
monotonic_array.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
# https://leetcode.com/problems/monotonic-array/
def is_monotonic(nums: list[int]) -> bool:
"""
Check if a list is monotonic.
>>> is_monotonic([1, 2, 2, 3])
True
>>> is_monotonic([6, 5, 4, 4])
True
>>> is_monotonic([1, 3, 2])
False
>>> is_monotonic([1,2,3,4,5,6,5])
False
>>> is_monotonic([-3,-2,-1])
True
>>> is_monotonic([-5,-6,-7])
True
>>> is_monotonic([0,0,0])
True
>>> is_monotonic([-100,0,100])
True
"""
return all(nums[i] <= nums[i + 1] for i in range(len(nums) - 1)) or all(
nums[i] >= nums[i + 1] for i in range(len(nums) - 1)
)
# Test the function with your examples
if __name__ == "__main__":
# Test the function with your examples
print(is_monotonic([1, 2, 2, 3])) # Output: True
print(is_monotonic([6, 5, 4, 4])) # Output: True
print(is_monotonic([1, 3, 2])) # Output: False
import doctest
doctest.testmod()