#234How big is the nucleus?Easy

How big is the nucleus?

Background

Top-p (nucleus) sampling keeps the smallest set of tokens whose probabilities sum to at least p. Its size adapts to confidence: a confident step needs only 1–2 tokens; an uncertain step may need dozens. (Top-k, by contrast, always keeps a fixed k.)

k(p)=min{k:j=1kp(j)p}k^*(p) = \min\Big\{k : \sum_{j=1}^{k} p_{(j)} \ge p\Big\}

where p_(1) ≥ p_(2) ≥ … are the probabilities sorted descending.

Problem statement

Implement nucleus_size(probs, p) returning how many tokens are in the nucleus.

Input

  • probs — list of token probabilities (sum ≈ 1, not necessarily sorted).
  • pfloat threshold in (0, 1].

Output

Returns an int: the number of top tokens whose cumulative probability first reaches p (at least 1).

Examples

Example 1 — confident step

Input:  probs = [0.9, 0.05, 0.03, 0.02], p = 0.9
Output: 1

Explanation: the top token alone reaches 0.9.

Example 2 — uncertain step

Input:  probs = [0.25, 0.25, 0.25, 0.25], p = 0.9
Output: 4

Constraints

  • Sort probabilities descending, accumulate until the sum ≥ p.
  • Return the count (always ≥ 1).

Notes

  • This adaptiveness is top-p's whole appeal over top-k: it widens on hard steps and narrows on easy ones.
Python
Loading...

▶ Run executes the 3 visible sample tests below in your browser. Submit runs the full suite — including hidden tests — on the server for an official verdict.

  • Example confident step -> 1
  • Reference uniform four -> 4
  • Sample needs two to cross 0.95