Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add solution isMonotonic python #43

Merged
merged 2 commits into from
May 5, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 21 additions & 1 deletion .github/workflows/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ on:
types: [opened, synchronize, reopened]

jobs:
test:
jest-tests:
name: Run Jest tests
runs-on: ubuntu-latest

Expand All @@ -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
2 changes: 1 addition & 1 deletion readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -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) |
Expand Down
1 change: 1 addition & 0 deletions requirements.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
pytest
26 changes: 26 additions & 0 deletions src/algorithms/arrays/is-monotonic/is_monotonic.py
Original file line number Diff line number Diff line change
@@ -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
15 changes: 15 additions & 0 deletions src/algorithms/arrays/is-monotonic/test_is_monotonic.py
Original file line number Diff line number Diff line change
@@ -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])