Skip to content

zerohertzLib.algorithm.bisect

Functions:

Name Description
bisect_left

Binary Search (left)

bisect_right

Binary Search (right)

bisect_left

bisect_left(sorted_list: list[int | float], value: int | float) -> int

Binary Search (left)

Parameters:

Name Type Description Default
sorted_list list[int | float]

정렬된 list

required
value int | float

찾고자하는 값

required

Returns:

Type Description
int

value 값이 sorted_list 에 삽입될 때 index

Examples:

>>> zz.algorithm.bisect_left([1, 3, 5], 2.7)
1
>>> zz.algorithm.bisect_left([1, 3, 5], 3)
1
>>> zz.algorithm.bisect_left([1, 3, 5], 3.3)
2
Source code in zerohertzLib/algorithm/bisect.py
def bisect_left(sorted_list: list[int | float], value: int | float) -> int:
    """Binary Search (left)

    Args:
        sorted_list: 정렬된 list
        value: 찾고자하는 값

    Returns:
        `value` 값이 `sorted_list` 에 삽입될 때 index

    Examples:
        >>> zz.algorithm.bisect_left([1, 3, 5], 2.7)
        1
        >>> zz.algorithm.bisect_left([1, 3, 5], 3)
        1
        >>> zz.algorithm.bisect_left([1, 3, 5], 3.3)
        2
    """
    low, high = 0, len(sorted_list)
    while low < high:
        mid = (low + high) // 2
        if sorted_list[mid] < value:
            low = mid + 1
        else:
            high = mid
    return low

bisect_right

bisect_right(sorted_list: list[int | float], value: int | float) -> int

Binary Search (right)

Parameters:

Name Type Description Default
sorted_list list[int | float]

정렬된 list

required
value int | float

찾고자하는 값

required

Returns:

Type Description
int

value 값이 sorted_list 에 삽입될 때 index

Examples:

>>> zz.algorithm.bisect_right([1, 3, 5], 2.7)
1
>>> zz.algorithm.bisect_right([1, 3, 5], 3)
2
>>> zz.algorithm.bisect_right([1, 3, 5], 3.3)
2
Source code in zerohertzLib/algorithm/bisect.py
def bisect_right(sorted_list: list[int | float], value: int | float) -> int:
    """Binary Search (right)

    Args:
        sorted_list: 정렬된 list
        value: 찾고자하는 값

    Returns:
        `value` 값이 `sorted_list` 에 삽입될 때 index

    Examples:
        >>> zz.algorithm.bisect_right([1, 3, 5], 2.7)
        1
        >>> zz.algorithm.bisect_right([1, 3, 5], 3)
        2
        >>> zz.algorithm.bisect_right([1, 3, 5], 3.3)
        2
    """
    low, high = 0, len(sorted_list)
    while low < high:
        mid = (low + high) // 2
        if value < sorted_list[mid]:
            high = mid
        else:
            low = mid + 1
    return low