PY-49

Classify Parsed Rows in Rule Order

  • Medium
  • Text Rows and Validation
  • Python

Task

Write classify_parsed_rows(records, allowed_labels). Each record is a dictionary containing source_line, record_id, label, and value. allowed_labels is a set of accepted non-empty labels.

Classify every record under this exact first-failure order:

  1. an empty label is missing: "missing label";
  2. a None value is missing: "missing value";
  3. a value below 0 or above 100 is rejected: "value out of range";
  4. a label absent from allowed_labels is rejected: "unknown label";
  5. otherwise the record is valid.

Return a dictionary with exactly valid, missing, and rejected. valid contains the original valid record dictionaries in source order. Each item in the other lists is this evidence tuple:

(source_line, record_id, reason)

Every input record must appear in exactly one result category.

Example

The second record is reported as missing, not out of range, because the first failing rule determines its category and evidence.

Your implementation

Edit solution.py and keep this function signature:

Return new outer collections. Do not change a record or either input, print, or ask for input.