From b83ff59bd474868bdb43559db2a7006025d3cd2e Mon Sep 17 00:00:00 2001 From: Khushi Shukla Date: Fri, 27 Oct 2023 13:32:42 +0530 Subject: [PATCH 1/3] Create monotonic_array.py --- data_structures/arrays/monotonic_array.py | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) create mode 100644 data_structures/arrays/monotonic_array.py diff --git a/data_structures/arrays/monotonic_array.py b/data_structures/arrays/monotonic_array.py new file mode 100644 index 000000000000..702cdad3ed54 --- /dev/null +++ b/data_structures/arrays/monotonic_array.py @@ -0,0 +1,19 @@ +# 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 + """ + 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 +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 From 67437a1fdca3a91511f776c9967a50a24d027313 Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Fri, 27 Oct 2023 08:04:39 +0000 Subject: [PATCH 2/3] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- data_structures/arrays/monotonic_array.py | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/data_structures/arrays/monotonic_array.py b/data_structures/arrays/monotonic_array.py index 702cdad3ed54..a968b8d7f5a7 100644 --- a/data_structures/arrays/monotonic_array.py +++ b/data_structures/arrays/monotonic_array.py @@ -10,10 +10,12 @@ def is_monotonic(nums: list[int]) -> bool: >>> is_monotonic([1, 3, 2]) False """ - 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)) + 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 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 +print(is_monotonic([1, 3, 2])) # Output: False From 82b4c59f94befbbff589d36d0ff5e3b4d3471aa7 Mon Sep 17 00:00:00 2001 From: Khushi Shukla Date: Fri, 27 Oct 2023 21:02:45 +0530 Subject: [PATCH 3/3] Update monotonic_array.py --- data_structures/arrays/monotonic_array.py | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/data_structures/arrays/monotonic_array.py b/data_structures/arrays/monotonic_array.py index a968b8d7f5a7..c50a21530814 100644 --- a/data_structures/arrays/monotonic_array.py +++ b/data_structures/arrays/monotonic_array.py @@ -16,6 +16,8 @@ def is_monotonic(nums: list[int]) -> bool: # 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 +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