모의고사

Sheryl Yun·2023년 8월 19일
0

처음 풀이

  • 문제에서 제공된 테스트 2개는 모두 통과했으나 제출 후 정확도 테스트에서 절반 이상이 나갔다.
  • 저 testee란 객체를 없애고 싶었고 이중 forEach문도 없애고 싶었으나 방법을 찾지 못했다. 마지막에 count for문은 continue를 쓰기 위한 for문 ㅜ_ㅜ
  • answers 길이가 최대 10,000이 될 수 있는데 저 길이만큼 돌리지 않고 답을 구할 수 있는 방법이 필요했다. while문을 떠올렸지만 어떻게 적용해야 할지..?
function solution(answers) {
    let answer = [];
    let count = [0, 0, 0];
    
    const testee = {
        '0': [1,2,3,4,5],
        '1': [2,1,2,3,2,4,2,5],
        '2': [3,3,1,1,2,2,4,4,5,5]
    }
    
    answers.forEach((answer, i) => {
        count.forEach((c, j) => {
          if (answer === testee[j][i]) count[j]++;
        })
    })
    
    let max = Math.max(...count);
    
    for (let i = 0; i < count.length; i++) {
        if (max !== count[i]) continue;
        else answer.push(i + 1)
    }
    
    return answer;
}

새로운 풀이

다른 사람 풀이 중 반복이 가장 적은 것을 골라 spread 추가 및 for문 -> forEach로 좀 더 읽기 좋게 만들었다.

function solution(answers) {
    let answer = [];
    const first = [1, 2, 3, 4, 5];
    const second = [2, 1, 2, 3, 2, 4, 2, 5];
    const third = [3, 3, 1, 1, 2, 2, 4, 4, 5, 5];
    let count = [0, 0, 0];

    answers.forEach((answer, i) => {
        if (answer == first[i % first.length]) count[0]++;
        if (answer == second[i % second.length]) count[1]++;
        if (answer == third[i % third.length]) count[2]++;
    })

    const max = Math.max(...count);
    
    count.forEach((c, i) => max == c && answer.push(i + 1))

    return answer;
}3
profile
영어강사, 프론트엔드 개발자를 거쳐 데이터 분석가를 준비하고 있습니다 ─ 데이터분석 블로그: https://cherylog.tistory.com/

0개의 댓글