PY-43

Decode UTF-8 and Record Failures

  • Easy–Medium
  • Text and Bytes
  • Python

Task

Write decode_utf8_inputs(inputs). inputs is a list of (source_id, payload) tuples. Each payload is a bytes value.

Decode every payload with strict UTF-8. Return (accepted, rejected) without stopping after a bad payload. Each accepted item is:

{"id": source_id, "text": decoded_text, "byte_count": len(payload)}

If decoding raises UnicodeDecodeError, append this stable rejected record:

Preserve input order within both lists. Catch only UnicodeDecodeError; an unrelated failure must remain visible.

Example

The rejected record keeps the source identity and byte span of the failed sequence. It does not replace invalid bytes or claim that decoding succeeded.

Your implementation

Edit solution.py and keep this function signature:

Return two new lists containing new dictionaries. Do not modify the input, print, or ask for input.