题解 | #简单错误记录#
简单错误记录
https://www.nowcoder.com/practice/2baa6aba39214d6ea91a2e03dff3fbeb
const rl = require("readline").createInterface({ input: process.stdin })
class Developer {
constructor() {
this.list = []
this.cache = {}
}
get listLen () {
return this.list.length
}
input (line) {
const [path, num] = line.split(' ')
if (!path) return
const fileName = this.getFileName(path)
const key = `${fileName}${num}`
if (this.cache[key]) {
this.cache[key].count ++
} else {
const obj = Object.create(null)
obj.fileName = fileName
obj.num = num
obj.count = 1
this.cache[key] = obj
this.list.push(obj)
}
}
output () {
if (!this.listLen) return
let len = 8
while (len > 0) {
if (len > this.listLen) {
len = this.listLen
}
const obj = this.list[this.listLen - len]
console.log(`${obj.fileName} ${obj.num} ${obj.count}`)
len --
}
}
getFileName (path, bit = 16) {
const items = path.split('\\')
const name = items[items.length - 1]
return name.length <= bit ? name : name.slice(0 - bit)
}
}
const developer = new Developer()
rl.on('line', (line) => {
if (line === 'end') {
rl.close()
return
}
developer.input(line)
})
rl.on('close', () => {
developer.output()
})

