题解 | #简单错误记录#

简单错误记录

https://www.nowcoder.com/practice/2baa6aba39214d6ea91a2e03dff3fbeb

const rl = require("readline").createInterface({ input: process.stdin });
var iter = rl[Symbol.asyncIterator]();
const readline = async () => (await iter.next()).value;

void (async function () {
  let inputs = [];
  while ((line = await readline())) {
    inputs.push(line);
  }
  // 循环记录去重,出现重复就是循环记录
  const lastIndex = inputs.lastIndexOf(inputs[0]);
  if (lastIndex !== 0) {
    inputs = inputs.slice(0, lastIndex);
  }
  inputs = inputs.map((item) => item.split(" "));
  // 超过16个字符的文件名称,只记录文件的最后有效16个字符;
  inputs.forEach((item) => {
    const str = item[0].match(/\\(\w+)$/)[1];
    item[0] = str.substring(str.length - 16); // 不足 16 为负数,substring 负数入参从 0 开始截取
  });
  // 统计重复出现次数,文件名 + 行号重复表示重复
  const count = inputs.reduce((res, cur) => {
    const key = cur[0] + " " + cur[1];
    res[key] ? res[key]++ : (res[key] = 1);
    return res;
  }, {});
  let result = Object.entries(count);
  // 只用输出最后出现的八条错误记录
  if (result.length > 8) result = result.slice(result.length - 8);
  result.forEach((item) => console.log(item[0] + " " + item[1])
  );
})();

全部评论

相关推荐

2025-12-31 14:31
湖南科技大学 Web前端
是阿亮吖:一个是这个时间招人比较少,另一个是沟通太少了。六十多份太养生了,最起码沟通个五六百份吧
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务