PY-51

Save Partitions and Check Every Row

  • Medium
  • Files and Data Integrity
  • Python

Task

Write save_partitions(base_path, source_rows, partitions). source_rows is a list of unique (record_id, text) tuples. partitions is a dictionary mapping a partition name to a list of tuples in the same form. IDs and text are strings; they contain no tabs or line-ending characters. Partition names are safe file stems made from letters, digits, hyphens, and underscores.

Before writing, account for every partition row:

  • missing: source IDs absent from all partitions;
  • duplicates: IDs appearing more than once across partitions;
  • extra: partition IDs absent from the source;
  • changed: source IDs whose partition text differs from the source text.

If any evidence set is non-empty, write nothing and return:

Otherwise create base_path, write each partition to <name>.tsv in dictionary insertion order, and encode as UTF-8. Every row is exactly record_id, one tab, text, and one \n. Reload every written file and check that its rows exactly equal the supplied partition rows. On success return the same shape with status "ok", the relative filenames in write order, row_count == len(source_rows), and four empty evidence sets. If reloaded rows differ, return status "reload mismatch", keep the filenames, set row count to the number reloaded, and put the differing known IDs in changed.

Example

A successful call writes train.tsv and test.tsv, reloads two rows, and returns files == ["train.tsv", "test.tsv"] with status "ok". Source order does not determine partition order; exact identity and unchanged row content determine validity.

Your implementation

Edit solution.py and keep this function signature:

You may import Path from pathlib. Do not change either input, print, or ask for input.