Apply a set of cleaning rules to a table, fill the gaps, and report exactly what was changed.
What to build
A program that cleans a table of people and reports the fixes per column.
Requirements
- Rules: an age must be a whole number (digits only) from 0 to 120, otherwise it is missing; an income must convert with
float()and be at least 0, otherwise it is missing; a city is stripped and title-cased (str.title()), and an empty city becomesUnknown. - Fill missing ages with the median of the valid ages and missing incomes with the mean of the valid incomes.
- Print each row as
ID,AGE,INCOME,CITY: the age without decimals if it is whole, otherwise with one; the income with two decimals. Then printfixed: age A, income I, city C(ages filled, incomes filled, and cities whose cleaned text differs from the original).
Input
Line 1: n. Then n lines id,age,income,city.
Output
n rows, then the report.
Constraints
1 ≤ n ≤ 1000; at least one valid age and one valid income.
Example
Input:
4
1,34,52000,dhaka
2,abc,48000, Sylhet
3,29,-5,
4,150,61000,Khulna
Output:
1,34,52000.00,Dhaka
2,31.5,48000.00,Sylhet
3,29,53666.67,Unknown
4,31.5,61000.00,Khulna
fixed: age 2, income 1, city 3
Why it works
Validating every field before filling means the fill values are computed only from trustworthy data, and counting the changes per column makes the cleaning auditable instead of silent.
Examples
4 1,34,52000,dhaka 2,abc,48000, Sylhet 3,29,-5, 4,150,61000,Khulna
1,34,52000.00,Dhaka 2,31.5,48000.00,Sylhet 3,29,53666.67,Unknown 4,31.5,61000.00,Khulna fixed: age 2, income 1, city 3
2 7,40,100,Rajshahi 8,40,200,Rajshahi
7,40,100.00,Rajshahi 8,40,200.00,Rajshahi fixed: age 0, income 0, city 0
Prompts inside input() are ignored when checking, so input("n: ") is fine.
Hints
Hint 1
Check each field against its rule first, keeping None for bad values; fill only after every row has been read.
Hint 2
statistics.median and a plain mean give the fill values; a city counts as fixed when the cleaned text differs from the original.
Solution
Show one correct solution (try it yourself first)
import re
import statistics
n = int(input())
rows = []
for _ in range(n):
rid, age, income, city = input().split(",")
age_value = int(age) if re.fullmatch(r"\d+", age) and int(age) <= 120 else None
try:
income_value = float(income)
if income_value < 0:
income_value = None
except ValueError:
income_value = None
clean_city = city.strip().title() or "Unknown"
rows.append([rid, age_value, income_value, clean_city, clean_city != city])
ages = [r[1] for r in rows if r[1] is not None]
incomes = [r[2] for r in rows if r[2] is not None]
age_fill = statistics.median(ages)
income_fill = sum(incomes) / len(incomes)
fixed_age = sum(1 for r in rows if r[1] is None)
fixed_income = sum(1 for r in rows if r[2] is None)
fixed_city = sum(1 for r in rows if r[4])
def show_age(a):
return str(int(a)) if a == int(a) else f"{a:.1f}"
for rid, age, income, city, _ in rows:
age = age_fill if age is None else age
income = income_fill if income is None else income
print(f"{rid},{show_age(age)},{income:.2f},{city}")
print(f"fixed: age {fixed_age}, income {fixed_income}, city {fixed_city}")