DSA/algorithms/Python/searching/binary_search.py

38 lines
948 B
Python
Raw Normal View History

from collections import namedtuple
TestData = namedtuple("TestData", "array target expected")
test_data = (
TestData([1, 2, 3, 4, 5], 1, True),
TestData([0, 4, 10, 1000], 10, True),
TestData([], -2, False),
)
2020-12-24 08:32:41 +00:00
def binary_search(array: list, target: int) -> bool:
"""
Search a sorted list to find a target integer
>>> all(binary_search(x.array, x.target) == x.expected for x in test_data)
True
"""
2020-12-24 08:32:41 +00:00
if len(array) < 1:
return False
if len(array) == 1:
return array[0] == target
mid = len(array) // 2
2020-12-24 08:32:41 +00:00
if array[mid] < target:
return binary_search(array[mid:], target)
elif array[mid] > target:
return binary_search(array[:mid], target)
else:
return True
2020-12-24 08:32:41 +00:00
if __name__ == "__main__":
for item in test_data:
print(
f"binary_search({item.array}, {item.target}) is "
f"{binary_search(item.array, item.target)}"
)