PY-41

Account for Every ID

  • Medium
  • Assertions and Invariants
  • Python

Task

Write audit_partition_ids(source_ids, partitions). source_ids is a list of unique IDs. partitions is a dictionary whose values are lists of output IDs; for example, its keys might be "train", "validation", and "test".

Every source ID should appear exactly once across all partition lists. Return a dictionary with exactly these four keys:

  • missing: source IDs that appear in no partition;
  • duplicates: IDs that appear more than once across the partition lists;
  • extra: partition IDs that do not appear in source_ids;
  • complete: True only when the other three sets are empty.

The first three values must be sets. Count every occurrence across every partition. An extra ID can therefore also be a duplicate. This rule accounts for every output position instead of stopping after the first problem.

Example

b occurs twice, c never appears, and x was not part of the source. A clean split reports three empty sets and complete equal to True.

Your implementation

Edit solution.py and keep this function name and signature:

IDs are hashable. The partition names do not affect the result. Do not change source_ids, partitions, or any partition list. Do not print or ask for input.