Question
There are three students who give up on studying mathematics because it is too hard. They will guess all of the answers to the questions for the exam of mathematic. Each of them will guess the answers with specific patterns. When the array includes the correct answers from the first question to the last question is given, write the code who get the highest result. The code will return an array include the students' number(s).
The patterns for guessing
- Student 1: 1, 2, 3, 4, 5, 1, 2, 3, 4, 5, ...
- Student 2: 2, 1, 2, 3, 2, 4, 2, 5, 2, 1, 2, 3, 2, 4, 2, 5, ...
- Student 3: 3, 3, 1, 1, 2, 2, 4, 4, 5, 5, 3, 3, 1, 1, 2, 2, 4, 4, 5, 5, ...
Limitation
- The number of questions cannot over 10,000.
- The answers can be one of 1, 2, 3, 4, and 5.
- If there are two or three students who got the same highest result, return the value in the order of ascending.
Example of I/O
answers | return | explanation |
[1, 2, 3, 4, 5] | [1] | Only student 1 guessed all correctly. |
[1, 3, 2, 4, 2] | [1, 2, 3] | Each student guessed two questions correctly. |
My solution
def solution(answers):
answer = []
student1 = [1, 2, 3, 4, 5]
student2 = [2, 1, 2, 3, 2, 4, 2, 5]
student3 = [3, 3, 1, 1, 2, 2, 4, 4, 5, 5]
point1 = 0
point2 = 0
point3 = 0
for index in range(len(answers)):
if answers[index] == student1[index % len(student1)]:
point1 += 1
if answers[index] == student2[index % len(student2)]:
point2 += 1
if answers[index] == student3[index % len(student3)]:
point3 += 1
highest_point = max(point1, point2, point3)
if highest_point == point1:
answer.append(1)
if highest_point == point2:
answer.append(2)
if highest_point == point3:
answer.append(3)
return answer
References
코딩테스트 연습 - 모의고사
수포자는 수학을 포기한 사람의 준말입니다. 수포자 삼인방은 모의고사에 수학 문제를 전부 찍으려 합니다. 수포자는 1번 문제부터 마지막 문제까지 다음과 같이 찍습니다. 1번 수포자가 찍는 ��
programmers.co.kr
'Problem solving > Algorithms' 카테고리의 다른 글
[LeetCode] 1480. Running Sum of 1d Array (Python) (0) | 2021.01.09 |
---|---|
An array of divisible numbers (나누어 떨어지는 숫자 배열) (0) | 2020.09.10 |
Finding prime numbers (소수 찾기) (0) | 2020.08.28 |
Sum between two integers (두 정수 사이의 합) (0) | 2020.08.28 |
Kth Number (K번째 수) (0) | 2020.08.27 |