PY-36
Join Record Collections by ID
Task
Write join_records_by_id(left, right) for two lists of record dictionaries.
Every record has an id key. Return a three-item tuple (pairs, status, details) and do not modify either input list or any record.
Validate the inputs in this order:
- If
leftrepeats an ID, return([], "duplicate left ids", duplicates).duplicatesis a list of each repeated ID at its first repeated occurrence, in left source order. - Otherwise, if
rightrepeats an ID, return([], "duplicate right ids", duplicates)using right source order. - Otherwise, if IDs from
leftare absent fromright, return([], "missing right ids", ids), with IDs in left source order. - Otherwise, if IDs from
rightare absent fromleft, return([], "extra right ids", ids), with IDs in right source order.
If none of those conditions applies, return (pairs, "ok", []). pairs is a
list of (left_record, right_record) tuples in left source order. Records are
matched by equal IDs. Stop at the first applicable condition; for example,
duplicate left IDs take precedence over every right-side condition.
Example
The record dictionaries themselves are returned in the pairs; make no copied or modified versions of them.
Your implementation
Edit solution.py and keep this function name and signature:
IDs are hashable. A duplicate is reported only once, when its second occurrence is encountered. Return the exact status strings and list forms described above.