Skip to content
dreamcode
dreamcode
Map
Weather Report
Section challenge · AdvancedPython
Reward: +80 XP
PROBLEM

Weather Report

A weather feed sends a list of dictionaries such as {"city": "Oslo", "temp": "3", "rain_mm": 1.2}. The values arrive as text, cities sometimes go missing, and rain is sometimes absent. Write summarize_readings(rows) that cleans the feed and reports on it.

Clean it in this order:

  1. Turn temp into numbers, with anything unreadable becoming missing.
  2. Drop every row with no city.
  3. Turn rain_mm into numbers and fill the gaps with 0.

Then return {"rows": how many rows are left, "avg_temp": the mean temperature rounded to one decimal place, "wettest": the city with the most rain}. If no rows survive, every value is 0 or None. If no temperature could be read, avg_temp is None. On a tie for the most rain, the first city wins.

Examples
summarize_readings([{'city': 'Oslo', 'temp': '3', 'rain_mm': 1.2}, {'city': 'Lima', 'temp': '19', 'rain_mm': None}, {'city': None, 'temp': '5', 'rain_mm': 9.9}, {'city': 'Bergen', 'temp': 'x', 'rain_mm': 7.5}])
{'rows': 3, 'avg_temp': 11, 'wettest': 'Bergen'}
summarize_readings([])
{'rows': 0, 'avg_temp': None, 'wettest': None}
solution.py
PYTHON
Saved as you type

Tests

0 of 5 passing
  • a messy feed
    summarize_readings([{'city': 'Oslo', 'temp': '3', 'rain_mm': 1.2}, {'city': 'Lima', 'temp': '19', 'rain_mm': None}, {'city': None, 'temp': '5', 'rain_mm': 9.9}, {'city': 'Bergen', 'temp': 'x', 'rain_mm': 7.5}])
    expected {'rows': 3, 'avg_temp': 11, 'wettest': 'Bergen'}
  • nothing to report
    summarize_readings([])
    expected {'rows': 0, 'avg_temp': None, 'wettest': None}
  • no readable temperature
    summarize_readings([{'city': 'A', 'temp': 'warm', 'rain_mm': 0}])
    expected {'rows': 1, 'avg_temp': None, 'wettest': 'A'}
  • a tie goes to the first city
    summarize_readings([{'city': 'A', 'temp': '10', 'rain_mm': 5}, {'city': 'B', 'temp': '20', 'rain_mm': 5}])
    expected {'rows': 2, 'avg_temp': 15, 'wettest': 'A'}
  • rounding to one place
    summarize_readings([{'city': 'A', 'temp': '1', 'rain_mm': 0}, {'city': 'B', 'temp': '2', 'rain_mm': 1}])
    expected {'rows': 2, 'avg_temp': 1.5, 'wettest': 'B'}
On the line
+80 XP
Pass all 5 tests to claim it.