pure python implementation of a binary search algorithm for doctests run following command python3 m doctest v simplebinarysearch py for manual testing run python3 simplebinarysearch py testlist 0 1 2 8 13 17 19 32 42 binarysearchtestlist 3 false binarysearchtestlist 13 true binarysearch4 4 5 6 7 4 true binarysearch4 4 5 6 7 10 false binarysearch18 2 18 true binarysearch5 5 true binarysearch a c d c true binarysearch a c d f false binarysearch 1 false binarysearch 1 1 8 1 true binarysearchrange5000 5000 10 80 true binarysearchrange5000 5000 10 1255 false binarysearchrange0 10000 5 2 false test_list 0 1 2 8 13 17 19 32 42 binary_search test_list 3 false binary_search test_list 13 true binary_search 4 4 5 6 7 4 true binary_search 4 4 5 6 7 10 false binary_search 18 2 18 true binary_search 5 5 true binary_search a c d c true binary_search a c d f false binary_search 1 false binary_search 1 1 8 1 true binary_search range 5000 5000 10 80 true binary_search range 5000 5000 10 1255 false binary_search range 0 10000 5 2 false
from __future__ import annotations


def binary_search(a_list: list[int], item: int) -> bool:
    
    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}")