[파이썬/python] 프로그래머스 : 모의고사 (Lv.1)

 

 

https://school.programmers.co.kr/learn/courses/30/lessons/42840

def solution(answers):
    one = [1,2,3,4,5]
    two = [2,1,2,3,2,4,2,5]
    three = [3,3,1,1,2,2,4,4,5,5]
    score = [0,0,0]
    ans = []
    for i in range(len(answers)):
        if answers[i] == one[i % len(one)]:
            score[0] += 1
        if answers[i] == two[i % len(two)]:
            score[1] += 1
        if answers[i] == three[i % len(three)]:
            score[2] += 1
    for i in range(len(score)):
        if max(score) == score[i]:
            ans.append(i+1)
    return ans

1번부터 3번 수포자들이 답을 찍는 일정한 패턴을 배열로 저장해 준 다음, i 번째의 답과 패턴의 i 번째(즉 i를 패턴의 길이로 나누었을 때의 나머지 번째)가 같으면 += 1을 해주었다.

나중에 출력할 때 편하게 하기 위해서 누적 점수가 들어가는 것을 [0,0,0] 형태의 배열로 만들어 주었다.

max(score)와 score[i]가 같으면 ans에 i+1을 출력해 주어서 수포자의 번호가 추가되도록 하였다.

comment