Jun 개발노트

프로그래머스 - 모의고사

2020-02-07

모의고사

const numOfsolution = (answer, man) => {
    let count = 0;
    for (let i = 0; i < answer.length; i++) {
        if (answer[i] === man[i % man.length]) {
            count++;
        }
    }
    return count;
};

const winner = answers => {
    const mans = [
        [1, 2, 3, 4, 5],
        [2, 1, 2, 3, 2, 4, 2, 5],
        [3, 3, 1, 1, 2, 2, 4, 4, 5, 5]
    ];
    const result = [];
    const line = [];
    let max = 0;
    for (let i in mans) {
        result.push(numOfsolution(answers, mans[i]));
    }

    for (let value of result) {
        if (max < value) {
            max = value;
        }
    }
    for (let i in result) {
        if (max === result[i]) {
            line.push(Number(i) + 1);
        }
    }
    line.sort((a, b) => a - b);
    return line;
};

test("모의고사", () => {
    expect(winner([1, 3, 2, 4, 2])).toEqual([1, 2, 3]);
});