Workspace/Coding labs
Loading progress

Build a group-safe dataset gate

Advanced60 min

Implement gate_examples(rows, heldout_groups) to validate records, protect evaluation groups, deduplicate canonical prompt-response pairs, and report deterministic reasons without mutating inputs.

Your task

  1. Each row is a dict with string id, group, prompt, and response. Missing, non-string, or blank required fields are invalid.
  2. Reject invalid rows first, then rows whose group is held out, then duplicate normalized prompt-response pairs. Normalize with NFKC, casefold, and whitespace collapse.
  3. Keep the first acceptable record for each pair in input order. A rejected held-out row must not consume a deduplication key.
  4. Return {"kept": [row ids], "rejected": [(row id or None, reason)]}, where reason is invalid, heldout, or duplicate. Inputs are not modified.

Examples

EXAMPLE 1

InputTwo rows with equivalent normalized prompt-response pairs

Output{"kept": ["first"], "rejected": [("second", "duplicate")]}

The first acceptable row wins; held-out rows never become representatives.
solution.pyPython 3.12