Each line of a log starts with a level in square brackets, such as [INFO] server started. Write count_levels(lines) that returns a dictionary mapping each level to the number of lines with that level.
A line is well formed only if it starts with [, contains a closing ], and the text between the brackets is non-empty and consists of uppercase letters only. Malformed lines are ignored. The message after the brackets may be anything, including empty.
Examples
Input: lines = ["[INFO] boot", "[ERROR] disk full", "[INFO] ready", "malformed line", "[warn] lowercase"]
Output: {"INFO": 2, "ERROR": 1}
Explanation: "malformed line" has no brackets and "[warn]" is not uppercase.
Input: lines = ["[]", "[A B] x"]
Output: {}
Constraints
0 <= len(lines) <= 10**4, each line printable ASCII of length at most 200- Target: O(total characters)
Goals
- Extract a bracketed prefix from a line
- Reject lines that do not follow the format
- Count occurrences with a dictionary