initial commit
This commit is contained in:
commit
89071996f3
160
.gitignore
vendored
Normal file
160
.gitignore
vendored
Normal file
@ -0,0 +1,160 @@
|
||||
# Byte-compiled / optimized / DLL files
|
||||
__pycache__/
|
||||
*.py[cod]
|
||||
*$py.class
|
||||
|
||||
# C extensions
|
||||
*.so
|
||||
|
||||
# Distribution / packaging
|
||||
.Python
|
||||
build/
|
||||
develop-eggs/
|
||||
dist/
|
||||
downloads/
|
||||
eggs/
|
||||
.eggs/
|
||||
lib/
|
||||
lib64/
|
||||
parts/
|
||||
sdist/
|
||||
var/
|
||||
wheels/
|
||||
share/python-wheels/
|
||||
*.egg-info/
|
||||
.installed.cfg
|
||||
*.egg
|
||||
MANIFEST
|
||||
|
||||
# PyInstaller
|
||||
# Usually these files are written by a python script from a template
|
||||
# before PyInstaller builds the exe, so as to inject date/other infos into it.
|
||||
*.manifest
|
||||
*.spec
|
||||
|
||||
# Installer logs
|
||||
pip-log.txt
|
||||
pip-delete-this-directory.txt
|
||||
|
||||
# Unit test / coverage reports
|
||||
htmlcov/
|
||||
.tox/
|
||||
.nox/
|
||||
.coverage
|
||||
.coverage.*
|
||||
.cache
|
||||
nosetests.xml
|
||||
coverage.xml
|
||||
*.cover
|
||||
*.py,cover
|
||||
.hypothesis/
|
||||
.pytest_cache/
|
||||
cover/
|
||||
|
||||
# Translations
|
||||
*.mo
|
||||
*.pot
|
||||
|
||||
# Django stuff:
|
||||
*.log
|
||||
local_settings.py
|
||||
db.sqlite3
|
||||
db.sqlite3-journal
|
||||
|
||||
# Flask stuff:
|
||||
instance/
|
||||
.webassets-cache
|
||||
|
||||
# Scrapy stuff:
|
||||
.scrapy
|
||||
|
||||
# Sphinx documentation
|
||||
docs/_build/
|
||||
|
||||
# PyBuilder
|
||||
.pybuilder/
|
||||
target/
|
||||
|
||||
# Jupyter Notebook
|
||||
.ipynb_checkpoints
|
||||
|
||||
# IPython
|
||||
profile_default/
|
||||
ipython_config.py
|
||||
|
||||
# pyenv
|
||||
# For a library or package, you might want to ignore these files since the code is
|
||||
# intended to run in multiple environments; otherwise, check them in:
|
||||
# .python-version
|
||||
|
||||
# pipenv
|
||||
# According to pypa/pipenv#598, it is recommended to include Pipfile.lock in version control.
|
||||
# However, in case of collaboration, if having platform-specific dependencies or dependencies
|
||||
# having no cross-platform support, pipenv may install dependencies that don't work, or not
|
||||
# install all needed dependencies.
|
||||
#Pipfile.lock
|
||||
|
||||
# poetry
|
||||
# Similar to Pipfile.lock, it is generally recommended to include poetry.lock in version control.
|
||||
# This is especially recommended for binary packages to ensure reproducibility, and is more
|
||||
# commonly ignored for libraries.
|
||||
# https://python-poetry.org/docs/basic-usage/#commit-your-poetrylock-file-to-version-control
|
||||
#poetry.lock
|
||||
|
||||
# pdm
|
||||
# Similar to Pipfile.lock, it is generally recommended to include pdm.lock in version control.
|
||||
#pdm.lock
|
||||
# pdm stores project-wide configurations in .pdm.toml, but it is recommended to not include it
|
||||
# in version control.
|
||||
# https://pdm.fming.dev/#use-with-ide
|
||||
.pdm.toml
|
||||
|
||||
# PEP 582; used by e.g. github.com/David-OConnor/pyflow and github.com/pdm-project/pdm
|
||||
__pypackages__/
|
||||
|
||||
# Celery stuff
|
||||
celerybeat-schedule
|
||||
celerybeat.pid
|
||||
|
||||
# SageMath parsed files
|
||||
*.sage.py
|
||||
|
||||
# Environments
|
||||
.env
|
||||
.venv
|
||||
env/
|
||||
venv/
|
||||
ENV/
|
||||
env.bak/
|
||||
venv.bak/
|
||||
|
||||
# Spyder project settings
|
||||
.spyderproject
|
||||
.spyproject
|
||||
|
||||
# Rope project settings
|
||||
.ropeproject
|
||||
|
||||
# mkdocs documentation
|
||||
/site
|
||||
|
||||
# mypy
|
||||
.mypy_cache/
|
||||
.dmypy.json
|
||||
dmypy.json
|
||||
|
||||
# Pyre type checker
|
||||
.pyre/
|
||||
|
||||
# pytype static type analyzer
|
||||
.pytype/
|
||||
|
||||
# Cython debug symbols
|
||||
cython_debug/
|
||||
|
||||
# PyCharm
|
||||
# JetBrains specific template is maintained in a separate JetBrains.gitignore that can
|
||||
# be found at https://github.com/github/gitignore/blob/main/Global/JetBrains.gitignore
|
||||
# and can be added to the global gitignore or merged into this file. For a more nuclear
|
||||
# option (not recommended) you can uncomment the following to ignore the entire idea folder.
|
||||
#.idea/
|
22
algorithms/binary_search.py
Normal file
22
algorithms/binary_search.py
Normal file
@ -0,0 +1,22 @@
|
||||
import pytest
|
||||
|
||||
|
||||
def binary_search(nums: list[int], target: int):
|
||||
low, high = 0, len(nums) - 1
|
||||
while low <= high:
|
||||
mid = low + (high - low) // 2
|
||||
if nums[mid] == target:
|
||||
return mid
|
||||
elif nums[mid] < target:
|
||||
low = mid + 1
|
||||
else:
|
||||
high = mid - 1
|
||||
return -1
|
||||
|
||||
|
||||
@pytest.mark.parametrize(
|
||||
"nums,target,expected",
|
||||
[([*range(5)], 2, 2), ([*range(5)], 6, -1), ([*range(20, 50)], 21, 1)],
|
||||
)
|
||||
def test_binary_search(nums: list[int], target: int, expected: int):
|
||||
assert binary_search(nums, target) == expected
|
35
leetcode/33_search_in_rotated_array.py
Normal file
35
leetcode/33_search_in_rotated_array.py
Normal file
@ -0,0 +1,35 @@
|
||||
import pytest
|
||||
|
||||
|
||||
class Solution:
|
||||
def search(self, nums: list[int], target: int) -> int:
|
||||
# The first item of first ascending sequence is always bigger than the first item of the second ascending sequence.
|
||||
low, high = 0, len(nums) - 1
|
||||
while low <= high:
|
||||
mid = low + (high - low) // 2
|
||||
if nums[mid] == target:
|
||||
return mid
|
||||
|
||||
# If the current item is bigger than the first item of the first ascending sequence we know that nums[low:mid] is sorted.
|
||||
# Otherwise we know that the current mid is on the edge of the second ascending sequence.
|
||||
if nums[mid] >= nums[low]:
|
||||
if nums[low] <= target <= nums[mid]:
|
||||
high = mid - 1
|
||||
else:
|
||||
low = mid + 1
|
||||
else:
|
||||
if nums[mid] <= target <= nums[high]:
|
||||
low = mid + 1
|
||||
else:
|
||||
high = mid - 1
|
||||
|
||||
return -1
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
def solution():
|
||||
return Solution()
|
||||
|
||||
|
||||
def test_search(solution: Solution):
|
||||
assert solution.search([4, 5, 6, 7, 0, 1, 2], 0) == 4
|
@ -0,0 +1,47 @@
|
||||
import typing
|
||||
import pytest
|
||||
|
||||
FindFn = typing.Callable[[int], bool]
|
||||
|
||||
|
||||
class Solution:
|
||||
def searchRange(self, nums: list[int], target: int) -> list[int]:
|
||||
start = self.binary_search(nums, target, lambda v: v >= target)
|
||||
end = self.binary_search(nums, target, lambda v: v > target)
|
||||
return [start, end]
|
||||
|
||||
def binary_search(self, nums: list[int], target: int, fn: FindFn) -> int:
|
||||
res = -1
|
||||
low, high = 0, len(nums) - 1
|
||||
while low <= high:
|
||||
mid = low + (high - low) // 2
|
||||
if nums[mid] == target:
|
||||
res = mid
|
||||
|
||||
if fn(nums[mid]):
|
||||
high = mid - 1
|
||||
else:
|
||||
low = mid + 1
|
||||
|
||||
return res
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
def solution():
|
||||
return Solution()
|
||||
|
||||
|
||||
@pytest.mark.parametrize(
|
||||
"nums, target, expected",
|
||||
[
|
||||
([5, 7, 7, 8, 8, 10], 8, [3, 4]),
|
||||
([5, 7, 7, 8, 8, 10], 6, [-1, -1]),
|
||||
([], 0, [-1, -1]),
|
||||
([1], 1, [0, 0]),
|
||||
([2, 2], 2, [0, 1]),
|
||||
],
|
||||
)
|
||||
def test_search_range(
|
||||
solution: Solution, nums: list[int], target: int, expected: list[int]
|
||||
):
|
||||
assert solution.searchRange(nums, target) == expected
|
2
pytest.ini
Normal file
2
pytest.ini
Normal file
@ -0,0 +1,2 @@
|
||||
[pytest]
|
||||
python_files = *.py
|
2
requirements.txt
Normal file
2
requirements.txt
Normal file
@ -0,0 +1,2 @@
|
||||
pytest
|
||||
black
|
Loading…
x
Reference in New Issue
Block a user