사용자 도구

사이트 도구


ps:teflib:binsearch_old

(old) binsearch.py

minimum_valid_integer v1.0

코드

# N minimum_valid_integer
# I {"version": "1.0", "typing": ["Callable"]}
def minimum_valid_integer(beg: int, end: int, is_valid: Callable[[int],
                                                                 bool]) -> int:
    ''' Returns the minimum int X in [beg, end) where is_valid(X) is true. '''
    if not is_valid(end - 1):
        raise ValueError
    while beg + 1 < end:
        mid = (beg + end - 1) >> 1
        if is_valid(mid):
            end = mid + 1
        else:
            beg = mid + 1
    return beg

maximum_valid_integer v1.0

코드

# N maximum_valid_integer
# I {"version": "1.0", "typing": ["Callable"]}
def maximum_valid_integer(beg: int, end: int, is_valid: Callable[[int],
                                                                 bool]) -> int:
    ''' Returns the maximum int X in [beg, end) where is_valid(X) is true. '''
    if not is_valid(beg):
        raise ValueError
    while beg + 1 < end:
        mid = (beg + end) >> 1
        if is_valid(mid):
            beg = mid
        else:
            end = mid
    return beg

토론

댓글을 입력하세요:
X T L᠎ Q C
 
ps/teflib/binsearch_old.txt · 마지막으로 수정됨: 2022/04/22 05:46 저자 teferi