Count status codes in an access log

easyPipelines~15 min · 7 tests

Web servers write one line per request. Before anything reaches a dashboard, something has to turn those lines into numbers, and real logs are never perfectly clean.

Write count_by_status(lines):

  • lines is a list of strings in the common log format:

    203.0.113.9 - - [04/Oct/2026:10:00:01 +0000] "GET /api/orders HTTP/1.1" 200 512
  • The status code is the first field after the quoted request.

  • Return a dict mapping each status code (an int) to how many lines had it.

  • Skip blank lines and malformed lines: no quoted request, or a status that isn’t exactly three digits.

  • The size field can be - (no body). That line still counts.

>>> count_by_status([
...     '1.2.3.4 - - [04/Oct/2026:10:00:01 +0000] "GET / HTTP/1.1" 200 512',
...     '1.2.3.4 - - [04/Oct/2026:10:00:02 +0000] "GET /x HTTP/1.1" 404 -',
...     'garbage',
... ])
{200: 1, 404: 1}
solution.pyPython 3.14 · Pyodide

Press ⌘ ↵ or Run to check the visible tests. Submit runs the hidden ones too. Everything runs in your browser.

⌘ ↵ to run