From 6fba7392e5b10239f6af778c796d9ee7e25fd28c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?R=C3=A4ghib=20Hasan?= Date: Sat, 4 May 2024 23:35:41 +0300 Subject: [PATCH 1/2] add solution isMonotonic python --- readme.md | 2 +- requirements.txt | 1 + .../arrays/is-monotonic/is_monotonic.py | 26 +++++++++++++++++++ .../arrays/is-monotonic/test_is_monotonic.py | 19 ++++++++++++++ 4 files changed, 47 insertions(+), 1 deletion(-) create mode 100644 requirements.txt create mode 100644 src/algorithms/arrays/is-monotonic/is_monotonic.py create mode 100644 src/algorithms/arrays/is-monotonic/test_is_monotonic.py diff --git a/readme.md b/readme.md index 1f82ab6..255004e 100644 --- a/readme.md +++ b/readme.md @@ -2,7 +2,7 @@ | Name | Tags | Solution | | --------------------------------------------------------- | ----------------------- | ----------------------------------------------------------------------- | -| Is-Monotonic | `Arrays` | [TypeScript](./src/algorithms/arrays/is-monotonic) | +| Is-Monotonic | `Arrays` | [TypeScript](./src/algorithms/arrays/is-monotonic/is-monotonic.ts) , [Python](./src/algorithms/arrays/is-montonic/is_monotonic.py) | | 2D Array - DS | `Arrays` | [TypeScript](./src/algorithms/arrays/2d-array-ds) | | Left Rotation | `Arrays` | [TypeScript](./src/algorithms/arrays/left-rotation) | | New Year Chaos | `Arrays` | [TypeScript](./src/algorithms/arrays/new-year-chaos) | diff --git a/requirements.txt b/requirements.txt new file mode 100644 index 0000000..55b033e --- /dev/null +++ b/requirements.txt @@ -0,0 +1 @@ +pytest \ No newline at end of file diff --git a/src/algorithms/arrays/is-monotonic/is_monotonic.py b/src/algorithms/arrays/is-monotonic/is_monotonic.py new file mode 100644 index 0000000..b70cc8c --- /dev/null +++ b/src/algorithms/arrays/is-monotonic/is_monotonic.py @@ -0,0 +1,26 @@ +""" +Is Monotonic + +An array is monotonic if it is either monotone increasing or monotone decreasing. + +An array nums is monotone increasing if for all i <= j, nums[i] <= nums[j]. + +An array nums is monotone decreasing if for all i <= j, nums[i] >= nums[j]. + +Given an integer array nums, return true if the given array is monotonic, or false otherwise. + +""" + +from typing import List + + +class Solution: + def isMonotonic(self, nums: List[int]) -> bool: + return True if (nums == sorted(nums) or nums == sorted(nums, reverse=True)) else False + + +class AnotherSolution: + def isMonotonic(self, nums: List[int]) -> bool: + is_increasing = all(nums[i] <= nums[i+1] for i in range(len(nums)-1)) + is_decreasing = all(nums[i] >= nums[i+1] for i in range(len(nums)-1)) + return is_increasing or is_decreasing diff --git a/src/algorithms/arrays/is-monotonic/test_is_monotonic.py b/src/algorithms/arrays/is-monotonic/test_is_monotonic.py new file mode 100644 index 0000000..d5d861a --- /dev/null +++ b/src/algorithms/arrays/is-monotonic/test_is_monotonic.py @@ -0,0 +1,19 @@ +import unittest +from is_monotonic import Solution + + +class TestIsMonotonic(unittest.TestCase): + + def setUp(self): + self.solution = Solution() + + def test_isMonotonic(self): + self.assertTrue(self.solution.isMonotonic([1, 2, 2, 3])) + + self.assertTrue(self.solution.isMonotonic([6, 5, 4, 4])) + + self.assertFalse(self.solution.isMonotonic([1, 3, 2])) + + +if __name__ == '__main__': + unittest.main() From b6dbbac81ec68503c0b48c61080e2738490de64e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?R=C3=A4ghib=20Hasan?= Date: Sun, 5 May 2024 11:34:14 +0300 Subject: [PATCH 2/2] use pytest --- .github/workflows/main.yml | 22 +++++++++++++++- .../arrays/is-monotonic/test_is_monotonic.py | 26 ++++++++----------- 2 files changed, 32 insertions(+), 16 deletions(-) diff --git a/.github/workflows/main.yml b/.github/workflows/main.yml index 89884ed..99574e9 100644 --- a/.github/workflows/main.yml +++ b/.github/workflows/main.yml @@ -5,7 +5,7 @@ on: types: [opened, synchronize, reopened] jobs: - test: + jest-tests: name: Run Jest tests runs-on: ubuntu-latest @@ -18,3 +18,23 @@ jobs: - name: Run Jest tests run: npm test -- --coverage --ci + + python-tests: + name: Run Python tests + runs-on: ubuntu-latest + + steps: + - name: Checkout repository + uses: actions/checkout@v4 + + - name: Set up Python + uses: actions/setup-python@v5 + with: + python-version: '3.10' + cache: 'pip' + + - name: Install dependencies + run: pip install -r requirements.txt + + - name: Run Python tests + run: pytest \ No newline at end of file diff --git a/src/algorithms/arrays/is-monotonic/test_is_monotonic.py b/src/algorithms/arrays/is-monotonic/test_is_monotonic.py index d5d861a..f80d334 100644 --- a/src/algorithms/arrays/is-monotonic/test_is_monotonic.py +++ b/src/algorithms/arrays/is-monotonic/test_is_monotonic.py @@ -1,19 +1,15 @@ -import unittest -from is_monotonic import Solution +from is_monotonic import Solution, AnotherSolution -class TestIsMonotonic(unittest.TestCase): +def test_solution_isMonotonic(): + solution = Solution() + assert solution.isMonotonic([1, 2, 2, 3]) + assert solution.isMonotonic([6, 5, 4, 4]) + assert not solution.isMonotonic([1, 3, 2]) - def setUp(self): - self.solution = Solution() - def test_isMonotonic(self): - self.assertTrue(self.solution.isMonotonic([1, 2, 2, 3])) - - self.assertTrue(self.solution.isMonotonic([6, 5, 4, 4])) - - self.assertFalse(self.solution.isMonotonic([1, 3, 2])) - - -if __name__ == '__main__': - unittest.main() +def test_another_solution_isMonotonic(): + another_solution = AnotherSolution() + assert another_solution.isMonotonic([1, 2, 2, 3]) + assert another_solution.isMonotonic([6, 5, 4, 4]) + assert not another_solution.isMonotonic([1, 3, 2])