반응형

 

출처: 프로그래머스 코딩 테스트 연습, https://programmers.co.kr/learn/challenges

 

 

 

 

** Javascript

 

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];
    
    const supoja1 = answers.filter((answer, index) => answer === first[index % first.length]).length;
    const supoja2 = answers.filter((answer, index) => answer === second[index % second.length]).length;
    const supoja3 = answers.filter((answer, index) => answer === third[index % third.length]).length;
    
    const max = Math.max(supoja1, supoja2, supoja3);

    if (supoja1 === max) {
        answer.push(1);
    }
    if (supoja2 === max) {
        answer.push(2);
    }
    if (supoja3 === max) {
        answer.push(3);
    }
    
    return answer;
}

 

 

 

** Python

 

def solution(answers):
    arr1 = [1, 2, 3, 4, 5]
    arr2 = [2, 1, 2, 3, 2, 4, 2, 5]
    arr3 = [3, 3, 1, 1, 2, 2, 4, 4, 5, 5]
    s = [0, 0, 0]
    
    for i, e in enumerate(answers):
        if e == arr1[i % len(arr1)]: s[0] += 1
        if e == arr2[i % len(arr2)]: s[1] += 1
        if e == arr3[i % len(arr3)]: s[2] += 1
    maxS = max(s)
    answer = [i + 1 for i, e in enumerate(s) if e == maxS]
    return answer

 

 

반응형

+ Recent posts