Skip to content

Corrected filename and include static types #2440

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 4 commits into from
Sep 17, 2020
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
26 changes: 0 additions & 26 deletions searches/simple-binary-search.py

This file was deleted.

45 changes: 45 additions & 0 deletions searches/simple_binary_search.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
"""
Pure Python implementation of the binary search algorithm.

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

For manual testing run:
python simple_binary_search.py
"""
from typing import List


def binary_search(a_list: List[int], item: int) -> bool:
"""
Pure python implementation of a binary search of a number is in a list.

>>> test_list = [0, 1, 2, 8, 13, 17, 19, 32, 42]
>>> print(binary_search(test_list, 3))
False
>>> print(binary_search(test_list, 13))
True
Copy link
Member

@cclauss cclauss Sep 16, 2020

Choose a reason for hiding this comment

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

Please add tests for negative integers, floats, repeated numbers, letters, empty list, single element list.

"""
if len(a_list) == 0:
return False
midpoint = len(a_list) // 2
if a_list[midpoint] == item:
return True
if item < a_list[midpoint]:
return binary_search(a_list[:midpoint], item)
else:
return binary_search(a_list[midpoint + 1:], item)


if __name__ == "__main__":
user_input: str = input("Enter numbers separated by comma:\n").strip()
sequence: List[int] = [int(item.strip()) for item in user_input.split(",")]
target_str = input("Enter the number to be found in the list:\n").strip()
target: int = int(target_str)
result = binary_search(sequence, target)
if result:
print(f"{target} was found in {sequence}")
else:
print(f"{target} was not found in {sequence}")