"""
Practice: is the change real, or noise?

Each clip has a number of reference words and a number of errors. Model A and model B were
run on the SAME clips, so errors_a[i] and errors_b[i] belong to the same clip.

Fill in the four TODOs, then run the tests:
    pytest test_stats_practice.py        (locally)
or press "Run tests" on the chapter page.
"""
import random


def corpus_wer(errors: list[int], words: list[int]) -> float:
    """Total errors / total words.

    TODO 1
    """
    return 0.0


def percentile(sorted_values: list[float], q: float) -> float:
    """Value at fraction q (0..1) of an already-sorted list (given)."""
    return sorted_values[min(len(sorted_values) - 1, int(q * len(sorted_values)))]


def bootstrap_ci(errors: list[int], words: list[int], n_boot: int = 1000, seed: int = 0) -> tuple[float, float]:
    """95% interval for corpus WER by resampling clips.

    n_boot times: draw len(errors) clip indexes WITH replacement (rng.randrange), sum their
    errors and words, record errors / words. Sort the results; return the 2.5% and 97.5% values.
    Use rng = random.Random(seed) so results are reproducible.

    TODO 2
    """
    return 0.0, 0.0


def paired_bootstrap(errors_a: list[int], errors_b: list[int], words: list[int],
                     n_boot: int = 1000, seed: int = 0) -> tuple[float, float]:
    """95% interval for the change in corpus WER, B - A.

    The key: draw ONE set of clip indexes per resample and use it for BOTH models, so a clip
    that's hard for A is hard for B in the same resample, and that shared difficulty cancels.

    TODO 3
    """
    return 0.0, 0.0


def verdict(lo: float, hi: float) -> str:
    """'better' if the whole interval of (B - A) is below 0, 'worse' if above, else "can't tell".

    TODO 4
    """
    return ""
