runtime
315 / 500
question 315 Iterators & Generators · medium

A generator that cleans taxi trips

TLC trip records arrive as CSV rows of text with real-world problems: blank distances, zero-mile trips, negative fares and clocks that put the drop-off before the pick-up. Write a generator clean_trips(rows) that yields one typed record per usable row and skips the rest, processing rows as they come.

Generatorclean_trips(rows); rows are dictionaries of text with trip_id, pickup_at, dropoff_at (YYYY-MM-DD HH:MM:SS), trip_distance_miles, fare_amount
Yields{"trip_id": int, "minutes": float, "miles": float, "fare": float}
Skipa row whose numbers or times can't be read, whose miles or fare is 0 or less, or whose drop-off isn't after the pick-up
Lazyit must be a generator that takes rows only as it needs them

Loading your progress…

A generator that cleans taxi trips — Python practice · Runtime