def bsearch(A, pred):
    """
    Assume the boolean function pred returns all False and then all True for
    values in A. Return the index of the first True, or len(A) if that does
    not exist.
    """
    # invariant: last False lies in [l, r) and A[l] is False
    if pred(A[0]):
        return 0
    l = 0
    r = len(A)
    while r-l > 1:
        m = l + (r-l)//2
        result = pred(A[m])
        if result:
            r = m
        else:
            l = m
    return l+1