Skip to content

Chore: Added type hints to searches/binary_search.py #2682

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

Merged
merged 5 commits into from
Dec 9, 2020
Merged
Changes from 4 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
74 changes: 28 additions & 46 deletions searches/binary_search.py
Original file line number Diff line number Diff line change
@@ -1,18 +1,21 @@
#!/usr/bin/env python3

"""
This is pure Python implementation of binary search algorithms

For doctests run following command:
python -m doctest -v binary_search.py
or
python3 -m doctest -v binary_search.py

For manual testing run:
python binary_search.py
python3 binary_search.py
"""
import bisect
from typing import List, Optional


def bisect_left(sorted_collection, item, lo=0, hi=None):
def bisect_left(
sorted_collection: List[int], item: int, lo: int = 0, hi: int = -1
) -> int:
"""
Locates the first element in a sorted array that is larger or equal to a given
value.
Expand Down Expand Up @@ -43,7 +46,7 @@ def bisect_left(sorted_collection, item, lo=0, hi=None):
>>> bisect_left([0, 5, 7, 10, 15], 6, 2)
2
"""
if hi is None:
if hi < 0:
hi = len(sorted_collection)

while lo < hi:
Expand All @@ -56,7 +59,9 @@ def bisect_left(sorted_collection, item, lo=0, hi=None):
return lo


def bisect_right(sorted_collection, item, lo=0, hi=None):
def bisect_right(
sorted_collection: List[int], item: int, lo: int = 0, hi: int = -1
) -> int:
"""
Locates the first element in a sorted array that is larger than a given value.

Expand Down Expand Up @@ -86,7 +91,7 @@ def bisect_right(sorted_collection, item, lo=0, hi=None):
>>> bisect_right([0, 5, 7, 10, 15], 6, 2)
2
"""
if hi is None:
if hi < 0:
hi = len(sorted_collection)

while lo < hi:
Expand All @@ -99,7 +104,9 @@ def bisect_right(sorted_collection, item, lo=0, hi=None):
return lo


def insort_left(sorted_collection, item, lo=0, hi=None):
def insort_left(
sorted_collection: List[int], item: int, lo: int = 0, hi: int = -1
) -> None:
"""
Inserts a given value into a sorted array before other values with the same value.

Expand Down Expand Up @@ -140,7 +147,9 @@ def insort_left(sorted_collection, item, lo=0, hi=None):
sorted_collection.insert(bisect_left(sorted_collection, item, lo, hi), item)


def insort_right(sorted_collection, item, lo=0, hi=None):
def insort_right(
sorted_collection: List[int], item: int, lo: int = 0, hi: int = -1
) -> None:
"""
Inserts a given value into a sorted array after other values with the same value.

Expand Down Expand Up @@ -181,7 +190,7 @@ def insort_right(sorted_collection, item, lo=0, hi=None):
sorted_collection.insert(bisect_right(sorted_collection, item, lo, hi), item)


def binary_search(sorted_collection, item):
def binary_search(sorted_collection: List[int], item: int) -> Optional[int]:
"""Pure implementation of binary search algorithm in Python

Be careful collection must be ascending sorted, otherwise result will be
Expand Down Expand Up @@ -219,7 +228,7 @@ def binary_search(sorted_collection, item):
return None


def binary_search_std_lib(sorted_collection, item):
def binary_search_std_lib(sorted_collection, item) -> Optional[int]:
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think you missed a few in here :)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good catch! Looks like @cclauss has taken care of this

"""Pure implementation of binary search algorithm in Python using stdlib

Be careful collection must be ascending sorted, otherwise result will be
Expand Down Expand Up @@ -248,7 +257,9 @@ def binary_search_std_lib(sorted_collection, item):
return None


def binary_search_by_recursion(sorted_collection, item, left, right):
def binary_search_by_recursion(
sorted_collection: List[int], item: int, left: int, right: int
) -> Optional[int]:

"""Pure implementation of binary search algorithm in Python by recursion

Expand Down Expand Up @@ -286,41 +297,12 @@ def binary_search_by_recursion(sorted_collection, item, left, right):
return binary_search_by_recursion(sorted_collection, item, midpoint + 1, right)


def __assert_sorted(collection):
"""Check if collection is ascending sorted, if not - raises :py:class:`ValueError`

:param collection: collection
:return: True if collection is ascending sorted
:raise: :py:class:`ValueError` if collection is not ascending sorted

Examples:
>>> __assert_sorted([0, 1, 2, 4])
True

>>> __assert_sorted([10, -1, 5])
Traceback (most recent call last):
...
ValueError: Collection must be ascending sorted
"""
if collection != sorted(collection):
raise ValueError("Collection must be ascending sorted")
return True


if __name__ == "__main__":
import sys

user_input = input("Enter numbers separated by comma:\n").strip()
collection = [int(item) for item in user_input.split(",")]
try:
__assert_sorted(collection)
except ValueError:
sys.exit("Sequence must be ascending sorted to apply binary search")

target_input = input("Enter a single number to be found in the list:\n")
target = int(target_input)
collection = sorted(int(item) for item in user_input.split(","))
target = int(input("Enter a single number to be found in the list:\n"))
result = binary_search(collection, target)
if result is not None:
print(f"{target} found at positions: {result}")
if result is None:
print(f"{target} was not found in {collection}.")
else:
print("Not found")
print(f"{target} was found at position {result} in {collection}.")