You are given a list of log strings logs, where each entry has the format "timestamp threadId message". Group the log entries by their thread ID.
Within each group, sort the entries by timestamp in ascending order. Return the result as a list of pairs [threadId, [sorted log entries]], with the outer list sorted lexicographically by thread ID.
The timestamp is a non-negative integer, the thread ID is a non-empty string, and the message is the rest of the log string after the first two space-separated tokens. The message can itself contain spaces.
Function Signature
from typing import List
def parse_logs(logs: List[str]) -> List[list]:
Rules
-
Each element of the result is a two-element list
[thread_id, entries]
, where
entries
holds the original log strings, unchanged.
-
Timestamps compare as integers, so
2
comes before
10
.
-
Thread IDs compare as plain strings, character by character, so
"worker10"
comes before
"worker2"
.
-
Entries of the same thread with equal timestamps keep their relative order from the input.
Constraints
-
1 <= len(logs) <= 10^4
-
Each log entry has the format
"timestamp threadId message"
.
-
timestamp
is a non-negative integer.
-
threadId
is a non-empty string.
-
message
is a non-empty string.
Examples
Input: logs = ["1 thread1 start process","3 thread2 init module","2 thread1 loading config","5 thread2 module ready","4 thread1 process complete"]
Output: [["thread1",["1 thread1 start process","2 thread1 loading config","4 thread1 process complete"]],["thread2",["3 thread2 init module","5 thread2 module ready"]]]
thread1 has entries at timestamps 1, 2 and 4, and thread2 has entries at 3 and 5. Each group is sorted by timestamp, and thread1 sorts before thread2.
Input: logs = ["10 alpha hello world","5 beta foo bar","1 alpha starting up","8 beta checking status"]
Output: [["alpha",["1 alpha starting up","10 alpha hello world"]],["beta",["5 beta foo bar","8 beta checking status"]]]
alpha has timestamps 10 and 1, which sort to 1, 10. beta has 5 and 8, already in order.
Input: logs = ["7 worker2 retry","7 worker10 boot","7 worker2 connect","12 worker2 done","3 worker2 queued"]
Output: [["worker10",["7 worker10 boot"]],["worker2",["3 worker2 queued","7 worker2 retry","7 worker2 connect","12 worker2 done"]]]
"worker10" sorts before "worker2" because the character 1 is smaller than 2. The worker2 timestamps sort as integers to 3, 7, 7, 12, so done goes last. The two entries at timestamp 7 keep their input order, so retry stays ahead of connect.