From 89071996f35a9542f4c530ae41f6df073a8c87f9 Mon Sep 17 00:00:00 2001 From: strNophix Date: Tue, 30 Aug 2022 13:16:48 +0200 Subject: [PATCH] initial commit --- .gitignore | 160 ++++++++++++++++++ algorithms/binary_search.py | 22 +++ leetcode/33_search_in_rotated_array.py | 35 ++++ ...ast_position_of_element_in_sorted_array.py | 47 +++++ pytest.ini | 2 + requirements.txt | 2 + 6 files changed, 268 insertions(+) create mode 100644 .gitignore create mode 100644 algorithms/binary_search.py create mode 100644 leetcode/33_search_in_rotated_array.py create mode 100644 leetcode/34_find_first_and_last_position_of_element_in_sorted_array.py create mode 100644 pytest.ini create mode 100644 requirements.txt diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..68bc17f --- /dev/null +++ b/.gitignore @@ -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/ diff --git a/algorithms/binary_search.py b/algorithms/binary_search.py new file mode 100644 index 0000000..208a0b2 --- /dev/null +++ b/algorithms/binary_search.py @@ -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 diff --git a/leetcode/33_search_in_rotated_array.py b/leetcode/33_search_in_rotated_array.py new file mode 100644 index 0000000..434eed1 --- /dev/null +++ b/leetcode/33_search_in_rotated_array.py @@ -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 diff --git a/leetcode/34_find_first_and_last_position_of_element_in_sorted_array.py b/leetcode/34_find_first_and_last_position_of_element_in_sorted_array.py new file mode 100644 index 0000000..b888789 --- /dev/null +++ b/leetcode/34_find_first_and_last_position_of_element_in_sorted_array.py @@ -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 diff --git a/pytest.ini b/pytest.ini new file mode 100644 index 0000000..7c47955 --- /dev/null +++ b/pytest.ini @@ -0,0 +1,2 @@ +[pytest] +python_files = *.py diff --git a/requirements.txt b/requirements.txt new file mode 100644 index 0000000..9bb2a15 --- /dev/null +++ b/requirements.txt @@ -0,0 +1,2 @@ +pytest +black