rohitg00/ai-engineering-from-scratch · error · ValueError

c cannot exceed n

Error message

c cannot exceed n

What it means

pass_at_k's combinatorial identity requires c <= n because you cannot have more correct samples than total samples; c > n indicates the caller mixed up argument order or merged counts from different tasks.

Solutions

  1. Keep (n, c) pairs together per task instead of assembling them from separate arrays
  2. Swap the arguments if they were passed as (c, n, k)
  3. Validate c <= n wherever you aggregate execution results
Defensive patterns

Strategy: validation

When it happens

Trigger: Thrown at phases/19-capstone-projects/72-code-exec-metric/code/main.py:257 when the library encounters an invalid state.

Common situations: See trigger scenarios.


AI-assisted analysis of rohitg00/ai-engineering-from-scratch@39ea8a1c6d (2026-08-26). Data as JSON: /api/errors/0591e14c89e51548. Report an issue: GitHub.

Appendix: source

Thrown at phases/19-capstone-projects/72-code-exec-metric/code/main.py:257

    if exit_code == EXIT_PASS and passed < total:
        exit_code = EXIT_ASSERTION_FAIL
    if exit_code == EXIT_ASSERTION_FAIL and passed == total and total > 0:
        exit_code = EXIT_PASS
    return ExecResult(score, exit_code, passed, total, detail=detail, per_assertion=results)


def score_code_exec(prediction: str, assertions: list[str], timeout_s: float = DEFAULT_TIMEOUT_S) -> float:
    code = extract_code(prediction)
    if code is None:
        return 0.0
    return run_candidate(code, assertions, timeout_s=timeout_s).score


def pass_at_k(n: int, c: int, k: int) -> float:
    if n < 0 or c < 0 or k <= 0:
        raise ValueError("n, c >= 0 and k > 0")
    if c > n:
        raise ValueError("c cannot exceed n")
    if k > n:
        return float(c > 0)
    if n - c < k:
        return 1.0
    log_prob = 0.0
    for i in range(k):
        log_prob += math.log(n - c - i) - math.log(n - i)
    return float(1.0 - math.exp(log_prob))


def pass_at_k_estimator(samples: list[list[bool]], ks: list[int]) -> dict[int, float]:
    out: dict[int, float] = {}
    for k in ks:
        per_task = []
        for sample in samples:
            n = len(sample)
            c = sum(1 for s in sample if s)
            per_task.append(pass_at_k(n, c, k))

View on GitHub (pinned to 39ea8a1c6d)