"""
Practice: waiting for copies. Lab 09's logic for scripts 02_sync_replication.py and 05_quorum.py
(quorum.py, staleness.py and consistency.py): the quorum rule, the coordinator's "first k
answers", the newest version, which reads were stale, a Wilson interval for a rate, which
settings survive a node down, and the longest stretch with no commit.

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


def overlaps(n, r, w):
    """The quorum rule, as in the lab's quorum.py: True if R + W > N.
    Raise ValueError unless 1 <= r <= n and 1 <= w <= n."""
    # TODO
    raise NotImplementedError


def every_read_meets_every_write(n, r, w):
    """Check the rule the long way: True if every set of r nodes out of n shares at least one
    node with every set of w nodes (itertools.combinations helps)."""
    # TODO
    raise NotImplementedError


def newest(answers):
    """The answer with the highest "version" (ties: the first). Raise ValueError if empty."""
    # TODO
    raise NotImplementedError


def first_k(answers, k):
    """The coordinator's wait, without asyncio. answers: one (finish_time, ok, value) per node,
    in any order. Go through them in finish order and return the values of the first k that
    succeeded. Raise RuntimeError as soon as too many have failed for k to be reached."""
    # TODO
    raise NotImplementedError


def newest_acked(read_sent, writes):
    """For each read's send time, the highest version acknowledged at or before it (-1 if none).
    writes: (done, version, ok) per write; only ok writes count (as in the lab's staleness.py)."""
    # TODO
    raise NotImplementedError


def stale_reads(reads, writes):
    """How many reads were stale. reads: (sent, version_got); writes: (done, version, ok).
    A read is stale if it got a version older than the newest one acknowledged before it was sent.
    If no write was acknowledged at all, return None: there's nothing to be stale against.
    (The lab's script reports 0 in that case, which reads like a result but isn't one.)"""
    # TODO
    raise NotImplementedError


def wilson(k, n, z=1.96):
    """The Wilson 95% interval (lo, hi) for k out of n, as in the lab's consistency.py:
    p = k/n, d = 1 + z²/n, centre = (p + z²/2n) / d, half = z·sqrt(p(1-p)/n + z²/4n²) / d,
    clipped to [0, 1]. Raise ValueError if n <= 0 or k is outside 0..n."""
    # TODO
    raise NotImplementedError


def survives(alive, r, w):
    """With `alive` nodes answering: {"writes": can a write get W acknowledgements,
    "reads": can a read get R answers}."""
    # TODO
    raise NotImplementedError


def longest_gap(ends):
    """The longest stretch with no commit finishing, as 02_sync_replication.py measures it:
    sort the commit end times and take the largest difference between neighbours.
    Raise ValueError with fewer than two commits."""
    # TODO
    raise NotImplementedError
