Structured Log Line Parser With Malformed-Row Recovery

Difficulty: Medium · Unit 4: Files, Exceptions & Standard Library · Featured in: CodeTantra Lab Pattern

You receive N log lines formatted as `LEVEL|SERVICE|LATENCY_MS` where `LEVEL` is `INFO`, `WARN`, or `ERROR`, `SERVICE` is non-empty, and `LATENCY_MS` is a non-negative integer. Skip malformed lines safely (counting how many were skipped) and report for each valid service its count and average latency truncated to an integer, sorted by service name.

Input & Output Format

Input: Line 1: integer N. Next N lines: raw log lines.

Output: Line 1: `SKIPPED <k>`. Following lines: `<SERVICE> <count> <avg_ms>` sorted alphabetically by SERVICE.

Sample 1

5
INFO|auth|120
BAD_LINE
ERROR|api|80
WARN|auth|abc
INFO|auth|60
SKIPPED 2
api 1 80
auth 2 90

BAD_LINE and WARN|auth|abc are malformed; auth avg = (120+60)//2 = 90.

Free forever — sign in with email at poorvithmp.com to unlock the dry run, verified Python 3 solution, edge-case traps, and downloads.

Back to Python Notes Hub