Debugging ยท Easy
Compare Two Datasets
Debug deterministic schema and aligned-row comparisons between datasets.
Task
Implement compare_two_datasets(reference_columns: np.ndarray, reference_rows: np.ndarray, candidate_columns: np.ndarray, candidate_rows: np.ndarray, tolerance: float) -> dict. Debug and repair a dataset comparison function so it returns the required deterministic schema and row-level equality summary.
Requirements
- The starter implementation runs, but contains several independent logic bugs. Fix it without changing the function signature or output keys.
- reference_columns and candidate_columns are one-dimensional arrays of unique integer column IDs and define the ordered schema of their respective datasets.
- Every row is rectangular: its length equals the number of columns in that dataset. Cell values are finite numbers; missing values, NaN, and infinity are outside this exercise.
- tolerance is finite and non-negative. Two aligned cells match when abs(reference - candidate) <= tolerance.
- The schemas match only when both column lists are exactly equal, including order. Report missing_columns in reference order and extra_columns in candidate order.
- Always report both row counts and unmatched_rows = abs(reference_rows - candidate_rows).
- Compare aligned rows only when the schemas match. In that case, compare the first min(reference_rows, candidate_rows) row pairs by position and report compared_rows, matching_rows, differing_rows, and differing_cells.
- When the schemas do not match, set all four row-comparison fields to 0; do not guess how renamed, missing, extra, or reordered columns should align.
- Return exactly the keys shown in the example, in that order. Empty datasets and empty schemas are valid, and neither input may be mutated.
Example
compare_two_datasets(
np.array([10, 20]),
np.array([[1.0, 0.8], [2.0, 0.4]]),
np.array([10, 20]),
np.array([[1.0, 0.8005], [2.0, 0.1], [3.0, 0.9]]),
0.001,
)
# {"schema_match": True, ..., "matching_rows": 1, "differing_rows": 1}