Data science: A cleaning pipeline with a log

Raw sales rows have stray spaces, inconsistent capitals, missing or broken amounts and duplicates. Clean them in a fixed order, log what each step did, and report totals per region.

What to build

A program that cleans sales rows, logs the cleaning and prints totals per region.

Requirements

  • Clean each row: strip spaces around every cell and write the region in title case (south west becomes South West).
  • Drop a row whose amount is empty or not a number. Then drop a row identical to an earlier kept row, comparing date, region and amount as a number.
  • Print read R, dropped D, duplicates U, kept K.
  • Then print REGION TOTAL for each region, total to two decimals, from the largest total to the smallest, and alphabetically on a tie.

Input

Line 1: n. Then n lines DATE,REGION,AMOUNT.

Output

One line, then one line per region.

Constraints

1 ≤ n ≤ 1000; regions are not empty; amounts are numbers, empty, or words (never nan or inf).

Example

Input:

7
2024-01-02, north ,120
2024-01-02,North,120
2024-01-03,SOUTH,80.5
2024-01-03,south,
2024-01-04,east,abc
2024-01-05,East,40
2024-01-06,north,30

Output:

read 7, dropped 2, duplicates 1, kept 4
North 150.00
South 80.50
East 40.00

Why it works

Cleaning before comparing makes rows that differ only in spacing or capitals count as duplicates, and counting every step tells the reader exactly which data the totals rest on.

Examples

Example 1 · Input
7
2024-01-02, north ,120
2024-01-02,North,120
2024-01-03,SOUTH,80.5
2024-01-03,south,
2024-01-04,east,abc
2024-01-05,East,40
2024-01-06,north,30
Expected output
read 7, dropped 2, duplicates 1, kept 4
North 150.00
South 80.50
East 40.00
Example 2 · Input
2
2024-02-01,West,10
2024-02-01,West,10.0
Expected output
read 2, dropped 0, duplicates 1, kept 1
West 10.00

Prompts inside input() are ignored when checking, so input("n: ") is fine.

Hints

Hint 1

Clean each row first: strip spaces from every cell and apply title() to the region.

Hint 2

An amount is valid when float() accepts it; catch ValueError.

Hint 3

Keep a set of cleaned (date, region, amount) tuples to spot duplicates.

Solution

Show one correct solution (try it yourself first)
n = int(input())
totals, seen = {}, set()
dropped = duplicates = 0
for _ in range(n):
    date, region, amount = [c.strip() for c in input().split(",")]
    region = region.title()
    try:
        value = float(amount)
    except ValueError:
        dropped += 1
        continue
    key = (date, region, value)
    if key in seen:
        duplicates += 1
        continue
    seen.add(key)
    totals[region] = totals.get(region, 0) + value
print(f"read {n}, dropped {dropped}, duplicates {duplicates}, kept {len(seen)}")
for region in sorted(totals, key=lambda r: (-totals[r], r)):
    print(f"{region} {totals[region]:.2f}")

Lesson: The data science workflow →