前端刷华为机考19-20题

144 阅读1分钟

HJ19 简单错误记录

const readline = require("readline");

const rl = readline.createInterface({
    input: process.stdin,
    output: process.stdout,
});
let dataList = [];
rl.on("line", function (line) {
    line.split("\n").forEach((record) => {
        let [filePath, lineNum] = record.split(" ");
        filePath = filePath.split("\\").pop().substr(-16);
        lineNum = +lineNum;

        const index = dataList.findIndex(
            (item) => item.filePath == filePath && item.lineNum == lineNum
        );
        if (index == -1) {
            dataList.push({ filePath, lineNum, count: 1 });
        } else {
            dataList[index].count++;
        }
    });
});

rl.on("close", () => {
    dataList.slice(-8).forEach((item) => {
        console.log(Object.values(item).join(" "));
    });
});

通过读题,我以为用这个模板就可以接收完数据...

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
    while(line = await readline()){
        let tokens = line.split(' ');
        let a = parseInt(tokens[0]);
        let b = parseInt(tokens[1]);
        console.log(a + b);
    }
}()

另外题目说"记录最多 8 条错误记录",我误用了shift方法。在后面的论述中,也能看出不妥,其实这里是指输出最后 8 条记录,

HJ20 密码验证合格程序

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
    while(line = await readline()){
        let isAllow = true;
        if(line.length > 8){
            let count = 0;

            if(line.match(/\d/)){
                count++;
            }
            if(line.match(/[a-z]/)){
                count++;
            }
            if(line.match(/[A-Z]/)){
                count++;
            }
            if(line.match(/[^\dA-z]/)){
                count++;
            }

            if(count < 3){
                isAllow = false;
            }

            if(line.match(/(.{3,}).*\1/)){
                isAllow = false;
            }
        }
        else{
            isAllow = false;
        }

        if(isAllow){
            console.log('OK');
        }
        else{
            console.log("NG");
        }
    }
}()

首先温习几个知识点:

  • \w 表示 a-z、A-Z、0-9,以及下划线

  • [^0-9A-z] 等价于 [^0-9^A-z]

  • /1 在正则表达式里代表匹配的第一个元组

只记得$1,想着判断是否符合条件三很麻烦的,看了下别的题解,吓了一跳,我验证是对的

const readline = require("readline");

const rl = readline.createInterface({
    input: process.stdin,
    output: process.stdout,
});
//个人觉得Js Node版本获取输入用起来比V8版本要麻烦一些
rl.on("line", function (line) {
    console.log(checkValid(line));
});

function checkValid(str) {
    let output = 'OK';
    //重点解释一下下面这条正则表达式,整体思想是排出单一种,单两种符号的全部组合,剩下的自然是三种以上符号的组合
    let regex = /^(?![A-Za-z]+$)(?![A-Z\d]+$)(?![A-Z\W]+$)(?![a-z\d]+$)(?![a-z\W]+$)(?![\d\W]+$)\S{8,}$/
    if (!str.match(regex)) {
        output = 'NG';
    }
    //这里没有使用正则表达式,因为在用例中会出现特殊符号如‘(’等,如果把该符号直接转成正则表达式,则会报错,故采用includes方法
    for (let i = 0; i < str.length-3; i++) {
        let newStr1 = str.slice(i, i+3); newStr2 = str.slice(i+3);
        if (newStr2.includes(newStr1)) {
            output = 'NG';
            break;
        }
    }
    return output;
}

离职在家的第 192 天