Summarise the lines of a server log.
What to build
A program that pulls structure out of raw lines of text.
Requirements
- Each line has a date, a level and a message, separated by spaces. The message can contain spaces.
- Print each level and its count, with the levels in alphabetical order.
- Print
First error: MESSAGE, orNo errorsif there are no lines with the levelERROR.
Input
The first line has n. Each of the next n lines is a log line.
Output
One line per level, then one more line.
Constraints
n is from 1 to 1000.
Example
Input:
3
2026-01-05 INFO server started
2026-01-05 ERROR disk full
2026-01-06 ERROR out of memory
Output:
ERROR 2
INFO 1
First error: disk full
Why it works
The limit in split(" ", 2) splits only twice, so the message keeps its own spaces. Sorting the dictionary's keys gives the alphabetical order.
Examples
Example 1 · Input
3 2026-01-05 INFO server started 2026-01-05 ERROR disk full 2026-01-06 ERROR out of memory
Expected output
ERROR 2 INFO 1 First error: disk full
Example 2 · Input
1 2026-02-01 WARN low battery
Expected output
WARN 1 No errors
Prompts inside input() are ignored when checking, so input("n: ") is fine.
Hints
Hint 1
Use line.split(" ", 2), so that the message stays in one piece.
Hint 2
Count each level in a dictionary.
Hint 3
Remember only the first error message you meet.
Solution
Show one correct solution (try it yourself first)
n = int(input())
counts = {}
first_error = None
for _ in range(n):
date, level, message = input().split(" ", 2)
counts[level] = counts.get(level, 0) + 1
if level == "ERROR" and first_error is None:
first_error = message
for level in sorted(counts):
print(level, counts[level])
if first_error is None:
print("No errors")
else:
print("First error:", first_error)