나 개발자 할래요
[JS] 모의고사 본문
function solution(answers) {
const a = [1, 2, 3, 4, 5];
const b = [2, 1, 2, 3, 2, 4, 2, 5];
const c = [3, 3, 1, 1, 2, 2, 4, 4, 5, 5];
const result = [0, 0, 0];
for (let i = 0; i < answers.length; i++) {
if(a[i % 5] === answers[i]) result[0]++;
if(b[i % 8] === answers[i]) result[1]++;
if(c[i % 10] === answers[i]) result[2]++;
}
const answer = [];
const maxValue = Math.max(...result);
let index = 0
for(let i = 0; i < 3; i++) {
if(maxValue === result[i]) {
answer[index] = i + 1;
index++;
}
} return answer;
}
수포자 1 패턴: [1, 2, 3, 4, 5] (5개로 반복)
수포자 2 패턴: [2, 1, 2, 3, 2, 4, 2, 5] (8개로 반복)
수포자 3 패턴: [3, 3, 1, 1, 2, 2, 4, 4, 5, 5] (10개로 반복)
`i % 5`, `i % 8`, `i % 10`는 각 수포자의 패턴이 반복되기 때문에 사용하는 연산자입니다.
다른 사람 풀이
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;
}
function solution(answers) {
const A = '12345'.split('')
const B = '21232425'.split('')
const C = '3311224455'.split('')
const scores = [A,B,C]
.map(p => {
return answers.map((answer, i)=> answer === Number(p[i % p.length]))
.filter(c => c)
.length
})
.map((score, i) => ({id: i+1, score}))
.sort((a, b) => b.score - a.score)
const ret = []
while(scores.length){
const p = scores.shift()
if(ret[0] && ret[0].score !== p.score) break;
ret.push(p)
}
return ret.map(p => p.id).sort()
}
'개발자 되는 법... > 코딩테스트...' 카테고리의 다른 글
[JS] 덧칠하기 (0) | 2024.08.14 |
---|---|
[JS] 소수 만들기 (0) | 2024.08.13 |
[JS] 과일 장수 (0) | 2024.08.09 |
[JS] 카드 뭉치 (0) | 2024.08.08 |
[JS] 2016 (0) | 2024.08.01 |