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/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..f80d334 --- /dev/null +++ b/src/algorithms/arrays/is-monotonic/test_is_monotonic.py @@ -0,0 +1,15 @@ +from is_monotonic import Solution, AnotherSolution + + +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 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])