Clean Repeated Log Tokens
Context
A monitoring service stores each event label as a token in the order it appeared. Bursts can repeat the same label many times in a row, making a log excerpt noisy. Create a cleaned token sequence that keeps one label for each consecutive burst while retaining separate appearances when another label occurs between them.
Problem
Implement a function that receives an ordered list of log tokens and returns a new list with each maximal run of adjacent equal tokens reduced to one representative. Preserve the order of the retained tokens. Equal tokens separated by a different token must both remain in the result. The input may be empty, and the returned list must also be empty when no tokens are supplied.
Examples
tokens = [][]tokens = ["error"]["error"]tokens = ["info", "info", "warn", "info", "info", "info", "debug"]["info", "warn", "info", "debug"]Constraints
0 <= len(tokens) <= 10001 <= len(tokens[i]) <= 24 for every itokens[i]: alphabet lowercasetokens contains only lowercase letters.- Types:
tokensis str[]; result is str[]
Function signature
def compress_log_runs(tokens: list[str]) -> list[str]
tokenslist[str]- The log tokens in their recorded order.
- returns list[str]
- A new token list containing exactly one representative from every maximal run of adjacent equal input tokens, in original order.
Adapted from MBPP problem task_730 (CC BY 4.0). Rewritten, extended and verified by Iksha.