๋ณธ๋ฌธ ๋ฐ”๋กœ๊ฐ€๊ธฐ
Programmers

ํ”„๋กœ๊ทธ๋ž˜๋จธ์Šค 1๋‹จ๊ณ„ : ๋ชจ์˜๊ณ ์‚ฌ

by ์ฝ”ํŒŒ์นด 2022. 11. 24.
728x90

ํ”„๋กœ๊ทธ๋ž˜๋จธ์Šค 1๋‹จ๊ณ„ : ๋ชจ์˜๊ณ ์‚ฌ

์ฝ”๋”ฉํ…Œ์ŠคํŠธ ์—ฐ์Šต > ์™„์ „ํƒ์ƒ‰ > ๋ชจ์˜๊ณ ์‚ฌ


๐Ÿ’ฉ ๋ฌธ์ œ ์„ค๋ช…

์ˆ˜ํฌ์ž๋Š” ์ˆ˜ํ•™์„ ํฌ๊ธฐํ•œ ์‚ฌ๋žŒ์˜ ์ค€๋ง์ž…๋‹ˆ๋‹ค. ์ˆ˜ํฌ์ž ์‚ผ์ธ๋ฐฉ์€ ๋ชจ์˜๊ณ ์‚ฌ์— ์ˆ˜ํ•™ ๋ฌธ์ œ๋ฅผ ์ „๋ถ€ ์ฐ์œผ๋ ค ํ•ฉ๋‹ˆ๋‹ค. ์ˆ˜ํฌ์ž๋Š” 1๋ฒˆ ๋ฌธ์ œ๋ถ€ํ„ฐ ๋งˆ์ง€๋ง‰ ๋ฌธ์ œ๊นŒ์ง€ ๋‹ค์Œ๊ณผ ๊ฐ™์ด ์ฐ์Šต๋‹ˆ๋‹ค.

1๋ฒˆ ์ˆ˜ํฌ์ž๊ฐ€ ์ฐ๋Š” ๋ฐฉ์‹: 1, 2, 3, 4, 5, 1, 2, 3, 4, 5, ...
2๋ฒˆ ์ˆ˜ํฌ์ž๊ฐ€ ์ฐ๋Š” ๋ฐฉ์‹: 2, 1, 2, 3, 2, 4, 2, 5, 2, 1, 2, 3, 2, 4, 2, 5, ...
3๋ฒˆ ์ˆ˜ํฌ์ž๊ฐ€ ์ฐ๋Š” ๋ฐฉ์‹: 3, 3, 1, 1, 2, 2, 4, 4, 5, 5, 3, 3, 1, 1, 2, 2, 4, 4, 5, 5, ...

1๋ฒˆ ๋ฌธ์ œ๋ถ€ํ„ฐ ๋งˆ์ง€๋ง‰ ๋ฌธ์ œ๊นŒ์ง€์˜ ์ •๋‹ต์ด ์ˆœ์„œ๋Œ€๋กœ ๋“ค์€ ๋ฐฐ์—ด answers๊ฐ€ ์ฃผ์–ด์กŒ์„ ๋•Œ, ๊ฐ€์žฅ ๋งŽ์€ ๋ฌธ์ œ๋ฅผ ๋งžํžŒ ์‚ฌ๋žŒ์ด ๋ˆ„๊ตฌ์ธ์ง€ ๋ฐฐ์—ด์— ๋‹ด์•„ return ํ•˜๋„๋ก solution ํ•จ์ˆ˜๋ฅผ ์ž‘์„ฑํ•ด์ฃผ์„ธ์š”.

๐Ÿ’ฉ ์ œํ•œ ์กฐ๊ฑด

โ˜๏ธ ์‹œํ—˜์€ ์ตœ๋Œ€ 10,000 ๋ฌธ์ œ๋กœ ๊ตฌ์„ฑ๋˜์–ด์žˆ์Šต๋‹ˆ๋‹ค.
โœŒ ๋ฌธ์ œ์˜ ์ •๋‹ต์€ 1, 2, 3, 4, 5์ค‘ ํ•˜๋‚˜์ž…๋‹ˆ๋‹ค.
๐Ÿ‘Œ ๊ฐ€์žฅ ๋†’์€ ์ ์ˆ˜๋ฅผ ๋ฐ›์€ ์‚ฌ๋žŒ์ด ์—ฌ๋Ÿฟ์ผ ๊ฒฝ์šฐ, returnํ•˜๋Š” ๊ฐ’์„ ์˜ค๋ฆ„์ฐจ์ˆœ ์ •๋ ฌํ•ด์ฃผ์„ธ์š”.

๐Ÿ’ฉ ๋ฌธ์ œ ํ’€์ด

function solution(answers) {
    let score1 = 0;
    let score2 = 0;
    let score3 = 0;
    let answer = "";
    let math1 = answer.padStart(answers.length, "12345").split("").map(e => Number(e));
    let math2 = answer.padStart(answers.length, "21232425").split("").map(e => Number(e));
    let math3 = answer.padStart(answers.length, "3311224455").split("").map(e => Number(e));
    
    for(i=0; i<answers.length; i++){
        if(math1[i] == answers[i]){score1++;}
        if(math2[i] == answers[i]){score2++;}
        if(math3[i] == answers[i]){score3++;}
    }
    
    let result = [];
    let genius = Math.max(score1, score2, score3);
    if(genius == score1){result.push(1)}
    if(genius == score2){result.push(2)}
    if(genius == score3){result.push(3)}
    
    return result;
}

๋‹ค ๋ฐ˜๋ณต์ธ๋ฐ for๋ฌธ ์“ฐ๊ธฐ ์• ๋งคํ•˜๊ฒŒ 3๋ฒˆ ๋ฐ˜๋ณต์ด๋ผ ์ฝ”๋“œ๊ฐ€ ์—„์ฒญ ๊ธฐ๋„ค์š”. padStart() ๋ฉ”์„œ๋“œ๋ฅผ ์‚ฌ์šฉํ•ด ๋ณด์•˜์Šต๋‹ˆ๋‹ค. for๋ฌธ ์† if๋ฌธ์œผ๋กœ ์ฑ„์ ์„ ํ•ด์ฃผ๊ณ , ๊ฒฐ๊ณผ๋Š” result์— ๋‹ด์•„์ค๋‹ˆ๋‹ค. ์ˆ˜ํฌ์ž์ธ๋ฐ genius๋Š” ๋ฌด์Šจ...
๊ทธ๋Ÿฐ๋ฐ ๋ณ€์ˆ˜๊ฐ€ ๋„ˆ๋ฌด ๋งŽ์•„์„œ let score1, score2, score3 = 0; ์ด๋ผ๊ณ  ํ•œ๋ฒˆ์— ์ฝ”๋“œ๋ฅผ ์ž‘์„ฑํ–ˆ์—ˆ๋Š”๋ฐ ์—๋Ÿฌ๊ฐ€ ๋‚˜๋Š” ์ด์œ ๊ฐ€ ๋ญ˜๊นŒ์š”?

๐Ÿ’ฉ ๋‹ค๋ฅธ ํ’€์ด

function solution(answers) {
    var answer = [];
    var a1 = [1, 2, 3, 4, 5];
    var a2 = [2, 1, 2, 3, 2, 4, 2, 5];
    var a3 = [3, 3, 1, 1, 2, 2, 4, 4, 5, 5];

    var a1c = answers.filter((a,i)=> a === a1[i%a1.length]).length;
    var a2c = answers.filter((a,i)=> a === a2[i%a2.length]).length;
    var a3c = answers.filter((a,i)=> a === a3[i%a3.length]).length;
    var max = Math.max(a1c,a2c,a3c);

    if (a1c === max) {answer.push(1)};
    if (a2c === max) {answer.push(2)};
    if (a3c === max) {answer.push(3)};

    return answer;
}

filter() ๋ฉ”์„œ๋“œ๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ OMR ๋งˆํ‚น์— ์ฑ„์ ๊นŒ์ง€ ํ•œ๋ฒˆ์— ๊ฐ™์ด ํ•ด์ฃผ์—ˆ๋„ค์š”. ๋Œ€๋‹จํ•ฉ๋‹ˆ๋‹ค. map() ์—ด์‹ฌํžˆ ๊ณต๋ถ€ํ–ˆ๋”๋‹ˆ, ์ด์ œ๋Š” filter()๋ฅผ ๋ฐฐ์›Œ์•ผ๊ฒ ๋„ค์š”.

728x90

๋Œ“๊ธ€

๊ฐ์‚ฌํ•ฉ๋‹ˆ๋‹ค. ๐Ÿฆ™

CSS
๊ด‘๊ณ  ์ค€๋น„์ค‘