How Dirty Is This Table?

Data ProcessingMedium+20 XP+20

How Dirty Is This Table?

Before a table goes anywhere near a model, it is worth asking one question: how dirty is it? Three numbers from the cleaning lessons answer it, and a team can track them every night as new rows arrive.

Task: write quality_report(columns, rows, rules).

  • columns is the list of column names.
  • rows is the table: a list of rows, where each row is a list with one value per column, in the same order as columns.
  • rules maps some of the column names to a validity rule. Columns with no rule are never checked. A rule takes one of two forms:
    • {"min": low, "max": high}: the value must be a number (an int or a float, so text such as "42" fails) with low <= value <= high.
    • {"allowed": [...]}: the value must be exactly one of the listed values, so "Gold" does not match "gold".

A cell is missing if it is None, or a string containing nothing but whitespace (such as "" or " ").

Return a tuple of three rates, each rounded to 4 decimal places:

  1. Completeness: the share of all cells (rows × columns) that are not missing.
  2. Duplicate rate: the share of rows that exactly repeat a row that appeared earlier in the table. The first appearance of a row is never a duplicate, and every later copy of it is.
  3. Validity: the share of checked cells that pass their rule. A checked cell is one that is not missing and sits in a column that has a rule. A missing cell is a completeness problem, not a validity problem, so it is left out here. If no cell gets checked at all, validity is 1.0.

Measure all three on the table exactly as given, duplicates included. The table always has at least one row.

Examples

columns = ["name", "age", "plan"]
rows = [["Ana", 34, "basic"], ["Ben", None, "plus"], ["Cy", 210, "basic"], ["Ana", 34, "basic"]]
rules = {"age": {"min": 0, "max": 120}, "plan": {"allowed": ["basic", "plus"]}}
returns (0.9167, 0.25, 0.8571)
columns = ["city", "temp_c", "status"]
rows = [["Oslo", 4.5, "ok"], [" ", 6.0, "ok"], ["Rome", "18", "ok"], ["Lima", 21.5, ""], ["Oslo", -3.0, "fault"]]
rules = {"temp_c": {"min": -40, "max": 50}, "status": {"allowed": ["ok", "fault"]}}
returns (0.8667, 0.0, 0.8889)

Hints

Solve it to unlock the solution

Once your code passes every test, a reference solution shows up here so you can compare it with yours.

solution.py
CtrlEnterto run