题解 | #配置文件恢复#
配置文件恢复
https://www.nowcoder.com/practice/ca6ac6ef9538419abf6f883f7d6f6ee5
const rl = require("readline").createInterface({ input: process.stdin }); var iter = rl[Symbol.asyncIterator](); const readline = async () => (await iter.next()).value; void (async function () { // Write your code here const isMatch = (str, key) => { let j = 0; for (let i of str) { if (i === key[j]) j++; else return false; } return true; }; let arrMap = new Map(); arrMap.set("reset board", "board fault"); arrMap.set("board add", "where to add"); arrMap.set("board delete", "no board at all"); arrMap.set("reboot backplane", "impossible"); arrMap.set("backplane abort", "install first"); while ((line = await readline())) { let tokens = line.split(" "); let final = "unknown command"; if (tokens.length === 1) { if (isMatch(line, "reset")) final = "reset what"; } else if (tokens.length === 2) { if (!( ((tokens[0] === "r" || tokens[0] === "re") && tokens[1] === "b") || (tokens[0] === "b" && tokens[1] === "a") )) { let mapKey = arrMap.keys(); while ((c = mapKey.next().value)) { let cArr = c.split(" "); if ( isMatch(tokens[0], cArr[0]) && isMatch(tokens[1], cArr[1]) ) { final = arrMap.get(c); break; } } } } console.log(final); } })();