"""Run: python algorithms.py. Standard library only."""
from collections import Counter, deque

def binary_search(values, target):
    """Return the first matching index, or -1. Input must be sorted."""
    left, right = 0, len(values)
    while left < right:
        mid = (left + right) // 2
        if values[mid] < target:
            left = mid + 1
        else:
            right = mid
    return left if left < len(values) and values[left] == target else -1

def bfs(graph, start):
    queue = deque([start])
    seen = {start}
    order = []
    while queue:
        node = queue.popleft()
        order.append(node)
        for neighbor in graph.get(node, []):
            if neighbor not in seen:
                seen.add(neighbor)
                queue.append(neighbor)
    return order

if __name__ == '__main__':
    values = [55, 68, 72, 81, 81, 95]
    for target in [0, 55, 81, 95, 100]:
        found = binary_search(values, target)
        assert found == (values.index(target) if target in values else -1)
    assert binary_search([], 1) == -1
    graph = {'A': ['B', 'C'], 'B': ['D'], 'C': ['D'], 'D': ['A']}
    assert bfs(graph, 'A') == ['A', 'B', 'C', 'D']
    print('first 81:', binary_search(values, 81))
    print('BFS:', bfs(graph, 'A'))
    print('counts:', dict(Counter(['pass', 'fail', 'pass'])))
