Skip to content
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
53 changes: 27 additions & 26 deletions .github/workflows/python-package-conda.yml
Original file line number Diff line number Diff line change
@@ -1,34 +1,35 @@
name: Python CI (conda)
name: Python Lint & Test

on: [push]

jobs:
build-linux:
build-python:
runs-on: ubuntu-latest
strategy:
max-parallel: 5
matrix:
python-version: ["3.10"]

steps:
- uses: actions/checkout@v3
- name: Set up Python 3.10
uses: actions/setup-python@v3
with:
python-version: '3.10'
- name: Add conda to system path
run: |
# $CONDA is an environment variable pointing to the root of the miniconda directory
echo $CONDA/bin >> $GITHUB_PATH
- name: Install dependencies
run: |
conda update -n base -c defaults conda
- name: Lint with flake8
run: |
conda install -y flake8
# stop the build if there are Python syntax errors or undefined names
flake8 . --count --select=E9,F63,F7,F82 --show-source --statistics
# exit-zero treats all errors as warnings. The GitHub editor is 127 chars wide
flake8 . --count --exit-zero --max-complexity=10 --max-line-length=127 --statistics
- name: Test with pytest
run: |
conda install -y pytest
pytest
- uses: actions/checkout@v4
- name: Set up Python ${{ matrix.python-version }}
uses: actions/setup-python@v5
with:
python-version: ${{ matrix.python-version }}
cache: "pip"

- name: Install dependencies
run: |
python -m pip install --upgrade pip
pip install -r python/requirements.txt

- name: Lint code with Ruff
run: ruff check python --output-format=github --target-version=py310

- name: Check code formatting with Ruff
run: ruff format python --diff --target-version=py310
continue-on-error: true

- name: Test with pytest
run: |
pip install pytest pytest-cov
pytest python --doctest-modules --junitxml=junit/test-results.xml --cov=python --cov-report=xml --cov-report=html
15 changes: 0 additions & 15 deletions problems/medium/container_with_most_water.py

This file was deleted.

24 changes: 0 additions & 24 deletions problems/medium/three_sum.py

This file was deleted.

3 changes: 3 additions & 0 deletions .gitignore → python/.gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
# Environment
*_env/

# Byte-compiled / optimized / DLL files
__pycache__/
*.py[cod]
Expand Down
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
Original file line number Diff line number Diff line change
Expand Up @@ -14,20 +14,20 @@ def isAlnumChar(c):
return True
return False

l, r = 0, len(s) - 1
left, right = 0, len(s) - 1
s = s.lower()

while l < r:
s_l, s_r = s[l], s[r]
while left < right:
s_l, s_r = s[left], s[right]
if not isAlnumChar(s_l):
l += 1
left += 1
continue
if not isAlnumChar(s_r):
r -= 1
right -= 1
continue

if s_l != s_r:
return False
l += 1
r -= 1
left += 1
right -= 1
return True
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ def correspondingBracket(c):
if close[i] == c:
return open_[i]
return None

stack = []
for c in s:
if isOpenBracket(c):
Expand Down
File renamed without changes.
12 changes: 6 additions & 6 deletions problems/hard/candy.py → python/problems/hard/candy.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,12 @@ def slope(i):

candies = [1] * n

for l in range(n - 1):
if steep_arr[l] == 1 and candies[l + 1] <= candies[l]:
candies[l + 1] = candies[l] + 1
for left in range(n - 1):
if steep_arr[left] == 1 and candies[left + 1] <= candies[left]:
candies[left + 1] = candies[left] + 1

for r in range(n - 1, 0, -1):
if steep_arr[r - 1] == -1 and candies[r] >= candies[r - 1]:
candies[r - 1] = candies[r] + 1
for right in range(n - 1, 0, -1):
if steep_arr[right - 1] == -1 and candies[right] >= candies[right - 1]:
candies[right - 1] = candies[right] + 1

return sum(candies)
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ class Solution:
def trap(self, height: List[int]) -> int:
n = len(height)

leftMax = height[0] # O(1) space
leftMax = height[0] # O(1) space
rightMax = height[n - 1]

left = 1
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ class Solution:
def trap(self, height: List[int]) -> int:
n = len(height)

leftmost = [0] * n # O(n) space
leftmost = [0] * n # O(n) space
rightmost = [0] * n

for i in range(1, n):
Expand Down
File renamed without changes.
15 changes: 15 additions & 0 deletions python/problems/medium/container_with_most_water.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
from typing import List


class Solution:
def maxArea(self, height: List[int]) -> int:
n = len(height)
left, right = 0, n - 1
ans = 0
while left < right:
ans = max(ans, min(height[right], height[left]) * (right - left))
if height[right] < height[left]:
right -= 1
else:
left += 1
return ans
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,15 @@

class Solution:
def lengthOfLongestSubstring(self, s: str) -> int:
l = 0
left = 0
count_map = collections.defaultdict(int)
max_length = 0

for r, c in enumerate(s):
count_map[c] += 1
while count_map[c] > 1:
count_map[s[l]] -= 1
l += 1
max_length = max(max_length, r - l + 1)
count_map[s[left]] -= 1
left += 1
max_length = max(max_length, r - left + 1)

return max_length
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
class MinStack:

def __init__(self):
self.stack = []

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,18 +10,18 @@ def minSubArrayLen(self, target, nums):
return 0

n = len(nums)
l, r = 0, 1
left, right = 0, 1
min_len = float("inf")
sub_sum = nums[0]
while r <= n:
while right <= n:
if sub_sum < target:
if r < n:
sub_sum += nums[r]
r += 1
if right < n:
sub_sum += nums[right]
right += 1
else:
diff = r - l
diff = right - left
if diff < min_len:
min_len = min(diff, min_len)
sub_sum -= nums[l]
l += 1
sub_sum -= nums[left]
left += 1
return 0 if min_len == float("inf") else min_len
24 changes: 24 additions & 0 deletions python/problems/medium/three_sum.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
from typing import List


class Solution:
def threeSum(self, nums: List[int]) -> List[List[int]]:
nums.sort()
n = len(nums)
res = []
for i in range(n - 2):
if i > 0 and nums[i] == nums[i - 1]:
continue
left, right = i + 1, n - 1
while left < right:
three_sum = nums[i] + nums[left] + nums[right]
if three_sum == 0:
res.append([nums[i], nums[left], nums[right]])
left += 1
while nums[left] == nums[left - 1] and left < right:
left += 1
elif three_sum < 0:
left += 1
else:
right -= 1
return res
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,13 @@

class Solution:
def twoSum(self, numbers: List[int], target: int) -> List[int]:
l, r = 0, len(numbers) - 1
while l < r:
tmp_sum = numbers[l] + numbers[r]
left, right = 0, len(numbers) - 1
while left < right:
tmp_sum = numbers[left] + numbers[right]
if tmp_sum == target:
return [l + 1, r + 1]
return [left + 1, right + 1]
elif tmp_sum < target:
l += 1
left += 1
else:
r -= 1
right -= 1
return [-1, -1]
File renamed without changes.
9 changes: 9 additions & 0 deletions python/requirements.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
# Upgrade pip to avoid compatibility issues
pip>=21.0

# Linting and formatting tools
ruff

# Testing tools
pytest
pytest-cov
File renamed without changes.
File renamed without changes.
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,17 @@

solution = Solution()


def test_one_duplicate():
sample = [1, 2, 3, 1]
assert solution.containsDuplicate(sample) == True
assert solution.containsDuplicate(sample)


def test_no_duplicates():
sample = [1, 2, 3, 4]
assert solution.containsDuplicate(sample) == False
assert not solution.containsDuplicate(sample)


def test_empty_array():
sample = []
assert solution.containsDuplicate(sample) == False
assert not solution.containsDuplicate(sample)
File renamed without changes.
File renamed without changes.
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,17 @@

solution = Solution()


def test_sample_one():
s = "abcabcbb"
assert solution.lengthOfLongestSubstring(s) == 3


def test_sample_two():
s = "bbbbb"
assert solution.lengthOfLongestSubstring(s) == 1


def test_sample_three():
s = "pwwkew"
assert solution.lengthOfLongestSubstring(s) == 3
assert solution.lengthOfLongestSubstring(s) == 3
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@


def test_trapping_rain_water():

# Test Case 1
height = [0, 1, 0, 2, 1, 0, 1, 3, 2, 1, 2, 1]
assert trap_o_n().trap(height) == 6
Expand Down
1 change: 1 addition & 0 deletions tests/test_two_sum.py → python/tests/test_two_sum.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ def test_sample_three():
target = 6
assert solution.twoSum(nums, target) == [0, 1]


def test_sample_four():
nums = [-3, 1, 2, 3]
target = 0
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,22 +2,26 @@

solution = Solution()


def test_anagram():
s = "anagram"
t = "nagaram"
assert solution.isAnagram(s, t) == True
assert solution.isAnagram(s, t)


def test_not_anagram():
s = "rat"
t = "car"
assert solution.isAnagram(s, t) == False
assert not solution.isAnagram(s, t)


def test_empty_strings():
s = ""
t = ""
assert solution.isAnagram(s, t) == True
assert solution.isAnagram(s, t)


def test_one_empty_string():
s = ""
t = "a"
assert solution.isAnagram(s, t) == False
assert not solution.isAnagram(s, t)
File renamed without changes.
File renamed without changes.
Loading