Imagine a log file that abridges chats. An example chat might be:
10:00 <john> hi!
10:01 <maria> hello!
10:07 <john> can you link the design?
where john said 6 total words and maria said 1.
Your goal is to present the top N most talkative users in descending order.
You need to write a function that takes an integer N and a filepath.
You are provided a helper function, parse_log, which returns a list of usernames and word counts for each message parsed from the provided file
[(‘john’, 1), (‘maria’, 1), (‘john’, 5)] in the example above.
Output: [“john”, “maria”]
这道谷歌真题给出了一份“精简版聊天日志”,每条记录只有时间、用户名和一句话。辅助函数 parse_log 已经帮你把日志解析成 (用户名, 本条消息的词数) 的列表,你需要在此基础上,把每个用户的总词数累加,然后按“总共说话最多”的顺序输出前 N 个用户名。核心考点是用哈希表统计、再根据计数进行排序,时间复杂度和代码可读性都要兼顾。