Compare commits

...

3 Commits

Author SHA1 Message Date
Melih Mehmet Sahin
24b83e8e59
Merge ce15f5921f into a19bede190 2024-11-01 13:50:04 +01:00
pre-commit-ci[bot]
ce15f5921f [pre-commit.ci] auto fixes from pre-commit.com hooks
for more information, see https://pre-commit.ci
2024-10-14 20:13:07 +00:00
Melih Mehmet Sahin
d9f2f9fb1e
Contributes to #9943 by adding tests to monotonic_array.py
Addeded doctest in the if __name__. Checks for negaitves and an array of same integers
2024-10-14 21:11:16 +01:00

View File

@ -9,6 +9,16 @@ def is_monotonic(nums: list[int]) -> bool:
True True
>>> is_monotonic([1, 3, 2]) >>> is_monotonic([1, 3, 2])
False 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( 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) nums[i] >= nums[i + 1] for i in range(len(nums) - 1)
@ -21,3 +31,7 @@ if __name__ == "__main__":
print(is_monotonic([1, 2, 2, 3])) # Output: True print(is_monotonic([1, 2, 2, 3])) # Output: True
print(is_monotonic([6, 5, 4, 4])) # Output: True print(is_monotonic([6, 5, 4, 4])) # Output: True
print(is_monotonic([1, 3, 2])) # Output: False print(is_monotonic([1, 3, 2])) # Output: False
import doctest
doctest.testmod()