[알고리즘] 완전탐색 프로그래머스 1단계 - 모의고사

minidoo·2020년 9월 19일
0

알고리즘

목록 보기
16/85
post-thumbnail
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 ]
    
    count1, count2, count3 = 0, 0, 0
    
    # 정답 배열 for문을 돌면서 일치하는 값이 있다면 count를 올린다.
    for i in range(len(answers)):
        if answers[i] == one[i%len(one)]:
            count1 += 1
        if answers[i] == two[i%len(two)]:
            count2 += 1
        if answers[i] == three[i%len(three)]:
            count3 += 1
    
    # 가장 많이 맞은 값을 구한다.
    max_num = max(count1, count2, count3) 
    answer = []
    if max_num == count1:
        answer.append(1)
    if max_num == count2:
        answer.append(2)
    if max_num == count3:
        answer.append(3)
    
    return answer

풀이과정

  1. 1번 수포자, 2번 수포자, 3번 수포자가 찍은 반복적인 값을 각각 one, two, three 배열에 넣는다.
  2. 정답지 배열을 돌면서 각 index가 정답과 일치하는지 확인 후, 그렇다면 count 값을 올려준다.
  3. 수포자 중 가장 많이 맞은 사람을 max 를 사용하여 구한다.
  4. max_num과 같은 사람을 answer 배열에 넣는다.

0개의 댓글