mirror of
https://github.com/TheAlgorithms/Python.git
synced 2024-11-27 15:01:08 +00:00
Corrected filename and include static types (#2440)
* Corrected name and include static types - The name of the file is now compliant with python naming conventions - Add static type as stated in contributing guidelines * Apply suggestions from code review - Delete documentation line to run doctests - Delete type hints for variables that comes from functions Co-authored-by: Christian Clauss <cclauss@me.com> * Add edge cases tests. * print(f"{target} was {not_str}found in {sequence}") Co-authored-by: Christian Clauss <cclauss@me.com>
This commit is contained in:
parent
1ac75f4683
commit
86fb2991d5
|
@ -1,26 +0,0 @@
|
|||
# A binary search implementation to test if a number is in a list of elements
|
||||
|
||||
|
||||
def binary_search(a_list, item):
|
||||
"""
|
||||
>>> 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
|
||||
"""
|
||||
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__":
|
||||
import doctest
|
||||
|
||||
doctest.testmod()
|
53
searches/simple_binary_search.py
Normal file
53
searches/simple_binary_search.py
Normal file
|
@ -0,0 +1,53 @@
|
|||
"""
|
||||
Pure Python implementation of a binary search algorithm.
|
||||
|
||||
For doctests run following command:
|
||||
python3 -m doctest -v simple_binary_search.py
|
||||
|
||||
For manual testing run:
|
||||
python3 simple_binary_search.py
|
||||
"""
|
||||
from typing import List
|
||||
|
||||
|
||||
def binary_search(a_list: List[int], item: int) -> bool:
|
||||
"""
|
||||
>>> 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
|
||||
>>> print(binary_search([4, 4, 5, 6, 7], 4))
|
||||
True
|
||||
>>> print(binary_search([4, 4, 5, 6, 7], -10))
|
||||
False
|
||||
>>> print(binary_search([-18, 2], -18))
|
||||
True
|
||||
>>> print(binary_search([5], 5))
|
||||
True
|
||||
>>> print(binary_search(['a', 'c', 'd'], 'c'))
|
||||
True
|
||||
>>> print(binary_search(['a', 'c', 'd'], 'f'))
|
||||
False
|
||||
>>> print(binary_search([], 1))
|
||||
False
|
||||
>>> print(binary_search([.1, .4 , -.1], .1))
|
||||
True
|
||||
"""
|
||||
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 = input("Enter numbers separated by comma:\n").strip()
|
||||
sequence = [int(item.strip()) for item in user_input.split(",")]
|
||||
target = int(input("Enter the number to be found in the list:\n").strip())
|
||||
not_str = "" if binary_search(sequence, target) else "not "
|
||||
print(f"{target} was {not_str}found in {sequence}")
|
Loading…
Reference in New Issue
Block a user