"""
Practice: right numbers, wrong answers. Lab 02's second half (02_hdr_histogram.py Exercise 2,
05_fanout.py, 06_aa_noise.py): why you can't average percentiles and how tallies (histograms)
add up instead, how often a page that waits for n calls hits a slow one, and how wide the
noise is when nothing changed.

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


def percentile(xs, q):
    """The q-th percentile (0-100), nearest-rank: sort the values and take the one at
    position ceil(q / 100 * n), counting from 1 (never below position 1). This is the
    lab's "exact" percentile (numpy's inverted_cdf)."""
    # TODO
    return 0.0


def mean_of_percentiles(windows, q):
    """What a dashboard that averages does: the q-th percentile of each window (a list
    of latencies), then the plain average of those numbers."""
    # TODO
    return 0.0


def tally(xs, edges):
    """A histogram: how many values fall in each range. `edges` are the ranges' upper
    bounds, ascending; a value goes in the first range whose upper bound is >= it.
    Return a list of counts, one per edge. (Values above the last edge are not expected.)"""
    # TODO
    return []


def merge(a, b):
    """Add two tallies with the same edges, range by range."""
    # TODO
    return []


def percentile_from_tally(counts, edges, q):
    """The q-th percentile read from a tally: find the range holding the value at
    position ceil(q / 100 * total) (as in percentile()) and return its upper bound."""
    # TODO
    return 0.0


def p_any_slow(n, p_slow=0.01):
    """The chance that at least one of n independent calls is slow, if each one is slow
    with probability p_slow (1% = slower than its own p99)."""
    # TODO
    return 0.0


def share_with_slow(pages, slow_line):
    """The share of pages (each a list of its calls' times) where at least one call took
    longer than slow_line."""
    # TODO
    return 0.0


def single_percentile_for_slowest(n, q=99):
    """If n calls are independent, the slowest of them is below t when all n are, so the
    slowest call's q-th percentile is one call's (q/100) ** (1/n) quantile. Return that
    quantile as a percentile (0-100)."""
    # TODO
    return 0.0


def spread(values):
    """How much identical runs moved: (max - min) / median."""
    # TODO
    return 0.0


def beats_noise(after, aa_values):
    """Does a new result fall outside the range the unchanged code produced in its A/A
    runs? (Necessary, not sufficient: then test it properly, several runs each.)"""
    # TODO
    return False
