def binary_search(sequence, item, start=0, end=None):
    """
    Returns the index of item in sequence if it exists,
    otherwise returns the index _would_ take if inserted.

    Searches between start (inclusive) and end (exclusive),
    which default to 0 and len(sequence) respectively.
    """ 

    if end is None:
        end = len(sequence)
    
    while start < end:
        mid = (start + end)//2

        if sequence[mid] > item:
            end = mid # Remember, end is exclsuive
        elif sequence[mid] < item:
            start = mid+1
        else: 
            return mid

    return start