Engineering Note

[Programmers] 모의고사 본문

Problem Solving/Programmers

[Programmers] 모의고사

Software Engineer Kim 2021. 11. 2. 20:15

문제

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

문제해결방법

  • 학생 3명이 모두 각각 패턴을 가지고 문제를 찍을 때 숫자들의 특징을 이용하면 쉽게 문제를 풀 수 있다.
1 2 3 4 5 6 7 8 9 10
5 4 3 2 1 5 1 2 4 1
2 1 2 3 2 4 2 5 2 1
  • 위의 표의 첫 줄은 문제의 번호를 나타내고 두 번째줄은 각 문제의 정답 세 번째 줄은 2번 학생의 정답 패턴을 나타낸 것이다. [2, 1, 2, 3, 2, 4, 2, 5] 8개의 숫자들을 가지고 반복하는 구조인데 이때 그러면 9번째는 1번 숫자와 갖고 10번째는 2번 숫자와 같다. 이러한 규칙을 활용할 수 있는것이 나머지연산이다. 1과 9는 8로 나누었을 때 나머지가 1로 같고, 2와 10은 8로 나누었을 때 나머지가 2로 같다. 이러한 숫자의 규칙을 활용하면 쉽게 풀 수 있다.
  • 이렇게 각 학생별로 맞춘 문제수를 구하고 최대로 맞춘 개수와 다른 학생들의 정답 개수를 비교해 같은 값이 존재하면 answer에 추가해주면 된다.

코드

def solution(answers):
    answer = []
    n = len(answers)
    student1_answer = [1, 2, 3, 4, 5]
    student2_answer = [2, 1, 2, 3, 2, 4, 2, 5]
    student3_answer = [3, 3, 1, 1, 2, 2, 4, 4, 5, 5]

    len_student1_answer = len(student1_answer)
    len_student2_answer = len(student2_answer)
    len_student3_answer = len(student3_answer)

    count1 = 0
    count2 = 0
    count3 = 0

    for question_number in range(len(answers)):
        if answers[question_number] == student1_answer[question_number%len_student1_answer]:
            count1 += 1
        if answers[question_number] == student2_answer[question_number%len_student2_answer]:
            count2 += 1
        if answers[question_number] == student3_answer[question_number%len_student3_answer]:
            count3 += 1

    answer_count = [count1, count2, count3]
    max_answer_count = max(answer_count)
    for person, score in enumerate(answer_count):
        if score == max_answer_count:
            answer.append(person+1)

    return answer

'Problem Solving > Programmers' 카테고리의 다른 글

[Programmers] 전화번호 목록  (0) 2022.01.11
[Programmers] 피로도  (0) 2022.01.03
스킬 체크 테스트 Level.1  (0) 2021.07.17
비밀지도  (0) 2021.07.17
네트워크  (0) 2021.06.27
Comments