#!/usr/bin/env python3
# Source: q6-experimentation.html
import numpy as np

def benjamini_hochberg(pvals: list[float], q: float = 0.05) -> np.ndarray:
    """Return a boolean mask of rejected hypotheses, in the input order."""
    p = np.asarray(pvals)
    m = len(p)
    order = np.argsort(p)
    thresh = q * np.arange(1, m + 1) / m
    passed = p[order] <= thresh
    reject = np.zeros(m, dtype=bool)
    if passed.any():
        k = np.max(np.nonzero(passed)[0])   # largest k that passes
        reject[order[: k + 1]] = True       # reject all up to k
    return reject

print(benjamini_hochberg([0.001, 0.008, 0.020, 0.041, 0.30]))
# [ True  True  True False False]
