Adding tests to monotonic_array.py (#12073)

* 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

* [pre-commit.ci] auto fixes from pre-commit.com hooks

for more information, see https://pre-commit.ci

---------

Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
This commit is contained in:
Melih Mehmet Sahin 2024-12-30 19:53:50 +00:00 committed by GitHub
parent 7e55fb6474
commit 8921b56a85
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

View File

@ -9,6 +9,16 @@ def is_monotonic(nums: list[int]) -> bool:
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)
@ -21,3 +31,7 @@ if __name__ == "__main__":
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()