PY-48

Parse Rows and Keep Source IDs

  • Medium
  • Text Rows and Parsing
  • Python

Task

Write parse_rows(text, delimiter). Each physical line should contain exactly three fields:

record_id<delimiter>label<delimiter>numeric_value

Split the input with text.splitlines(). Line numbers begin at 1. Preserve the unmodified split fields in a list called raw_fields, but strip surrounding whitespace from record_id, label, and the text passed to float.

Return (accepted, rejected). An accepted record has exactly these keys:

Reject under this exact first-failure order:

  1. field count is not three: "expected 3 fields";
  2. stripped record ID is empty: "empty record id";
  3. numeric conversion raises ValueError: "invalid numeric value".

A rejected record contains source_line, raw_fields, and reason. Continue after rejected rows and preserve source order within both result lists.

Example

The source line and raw fields remain available after conversion, so later reports can point back to the input evidence.

Your implementation

Edit solution.py and keep this function signature:

delimiter is a non-empty string. Return new lists and dictionaries. Do not print or ask for input.