Editor

Errors per Hour

#268 · Working with data · Intermediate · 60 XP · about 10 min

A server writes one line per event in the form 'HH:MM LEVEL message', for example '09:42 ERROR disk full'. HH is the hour (00 to 23) and MM the minute, both written with two digits. LEVEL is one of DEBUG, INFO, WARNING, ERROR or CRITICAL. The message can be any text, even words that are level names, and it can be empty, so a line may end right after the level.

Write errors_per_hour(log) that counts the lines whose level is ERROR or CRITICAL in each hour. Return a list of (hour, count) tuples for the hours that had at least one, in order of hour, with hour as a whole number. The lines all come from one day but are not necessarily in time order. Blank lines (empty, or only spaces) are skipped.

Examples

log = [
    '09:05 INFO server started',
    '09:42 ERROR disk full',
    '10:15 WARNING slow response',
    '09:59 CRITICAL database down',
    '10:20 INFO retry after ERROR',
    '14:03 ERROR timeout',
]
errors_per_hour(log)  # returns [(9, 2), (14, 1)]

The line at 10:20 mentions ERROR, but its level is INFO, so it does not count.

Constraints

  • 0 <= len(log) <= 100000

Fastest solvers

Leaderboard

First solves only, ranked by stars and then time.