def count_matches(values: list[int], target: int) -> tuple[int, int]:
    """Count matching entries and equality comparisons.

    Args:
        values: Integer category codes.
        target: Code to count.

    Returns:
        The match count and the comparison count.
    """
    matches = 0
    comparisons = 0
    for value in values:
        comparisons += 1
        if value == target:
            matches += 1
    return matches, comparisons


def count_equal_pairs(values: list[int]) -> tuple[int, int]:
    """Count equal pairs at distinct positions and comparisons.

    Args:
        values: Integer category codes.

    Returns:
        The equal-pair count and the comparison count.
    """
    pairs = 0
    comparisons = 0
    for left in range(len(values)):
        for right in range(left + 1, len(values)):
            comparisons += 1
            if values[left] == values[right]:
                pairs += 1
    return pairs, comparisons


def count_halvings(size: int) -> int:
    """Count integer halvings until a nonnegative size is at most one.

    Args:
        size: Initial nonnegative candidate count.

    Returns:
        The number of halvings.

    Raises:
        ValueError: If size is negative.
    """
    if size < 0:
        raise ValueError("size must be nonnegative")
    steps = 0
    while size > 1:
        size //= 2
        steps += 1
    return steps


codes = [4, 7, 4, 9, 7]
print("Matches and comparisons:", count_matches(codes, 4))
print("Pairs and comparisons:", count_equal_pairs(codes))
print("n | linear comparisons | pair comparisons | halvings")
for size in (4, 8, 16):
    values = list(range(size))
    print(size, count_matches(values, -1)[1],
          count_equal_pairs(values)[1], count_halvings(size), sep=" | ")

assert count_matches([], 4) == (0, 0)
assert count_matches([4], 4) == (1, 1)
assert count_equal_pairs([]) == (0, 0)
assert count_equal_pairs([4, 4, 4]) == (3, 3)
assert count_halvings(0) == 0
assert count_halvings(1) == 0
print("The empty, singleton, and duplicate cases pass.")
