"""
Practice: repeated runs. Lab 06's repeat math (evalkit/repeats.py and 01-03 scripts): which tasks are
flaky, which flipped between two runs, pass@k and pass^k from n runs per task, and a 95% confidence
interval two ways: naive (every run as a separate test case) and from the tasks.

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

A results matrix is a list of rows, one row per task, one 0/1 entry per run (1 = pass).
Every task has the same number of runs. Standard library only.
"""
import math

Z_95 = 1.959964  # the z value for a 95% interval, as in the lab


def flakiness(m):
    """Count tasks that passed every run, failed every run, or flipped (flaky).
    Return a dict {"always_pass": .., "always_fail": .., "flaky": ..}."""
    # TODO
    raise NotImplementedError


def flips(a, b):
    """Two runs of the same tasks, a and b (lists of 0/1, one per task).
    Return (passed_then_failed, failed_then_passed)."""
    # TODO
    raise NotImplementedError


def all_k_pass(p, k):
    """One task (or one shooter) with chance p per run: the chance all k runs pass."""
    # TODO
    raise NotImplementedError


def pass_at_k(m, k):
    """Chance at least one of k runs passes, averaged over tasks. Per task, from c passes in n runs:
    1 - C(n - c, k) / C(n, k)  (it counts the ways to pick k runs that are all failures).
    Raise ValueError if k > n."""
    # TODO
    raise NotImplementedError


def pass_hat_k(m, k):
    """pass^k: chance ALL k runs pass, averaged over tasks. Per task: C(c, k) / C(n, k).
    Raise ValueError if k > n."""
    # TODO
    raise NotImplementedError


def naive_ci(m):
    """The WRONG interval, for comparison: every run treated as a separate test case.
    p = all passes / all runs, N = tasks x runs, half-width = Z_95 * sqrt(p (1 - p) / N).
    Return (low, high)."""
    # TODO
    raise NotImplementedError


def task_level_ci(m):
    """The honest interval: built from each task's own pass rate (one number per task).
    mean of the per-task rates +/- Z_95 * SD(per-task rates, n - 1 in the denominator) / sqrt(tasks).
    Return (low, high)."""
    # TODO
    raise NotImplementedError


def effective_tasks(tasks, runs, icc):
    """How many independent tasks `tasks` x `runs` results are worth: tasks*runs / design effect,
    where design effect = 1 + (runs - 1) * icc (icc = the share of the variance that belongs to the task)."""
    # TODO
    raise NotImplementedError
