Description
In Korea, there are "people done with math" called "수포자" (pronounced "soo-po-ja"). "수포자" is an abbreviation for people who have given up on math. The three "수포자" want to guess all the math problems on a mock exam. They follow the following patterns:
Pattern for "수포자 1": 1, 2, 3, 4, 5, 1, 2, 3, 4, 5, ...
Pattern for "수포자 2": 2, 1, 2, 3, 2, 4, 2, 5, 2, 1, 2, 3, 2, 4, 2, 5, ...
Pattern for "수포자 3": 3, 3, 1, 1, 2, 2, 4, 4, 5, 5, 3, 3, 1, 1, 2, 2, 4, 4, 5, 5, ...
When given an array of answers from the first question to the last, called 'answers,' write a solution function to return an array containing the person or people who answered the most questions correctly. If multiple people have the same highest score, return the array in ascending order.
Restrictions:
- The exam consists of a maximum of 10,000 questions.
- The answers to the questions are either 1, 2, 3, 4, or 5.
- If multiple people receive the highest score, please return the values in ascending order.
Input/Output Example:
answer | return |
[1,2,3,4,5] | [1] |
[1,3,2,4,2] | [1,2,3] |
Example Input/Output #1:
수포자 1 answered all the questions correctly.
수포자 2 answered all the questions incorrectly.
수포자 3 answered all the questions incorrectly.
Therefore, the person who answered the most questions correctly is 수포자 1.
Example Input/Output #2:
All people answered 2 questions correctly.
Code:
def solution(answers):
answer = []
person = [0] * 3
a1 = [1, 2, 3, 4, 5]
a2 = [2, 1, 2, 3, 2, 4, 2, 5]
a3 = [3, 3, 1, 1, 2, 2, 4, 4, 5, 5]
len_a1 = len(a1)
len_a2 = len(a2)
len_a3 = len(a3)
for i in range(len(answers)):
if answers[i] == a1[i % len_a1]:
person[0] += 1
if answers[i] == a2[i % len_a2]:
person[1] += 1
if answers[i] == a3[i % len_a3]:
person[2] += 1
winner = max(person)
for i in range(len(person)):
if person[i] == winner:
answer.append(i + 1)
return answer
The solution function takes a list of answers as input. It initializes a list called answer to store the person(s) with the highest number of correct answers. The list person is created with three elements, representing the number of correct answers for each person. There are three answer lists: a1, a2, and a3, which correspond to the answers of the three persons respectively.
The code then iterates through the answers list. For each answer, it checks if it matches the corresponding answer in each person's answer list, using the modulo operator % to handle cases where the answer list needs to repeat. If there's a match, the respective person's count is incremented.
After counting the correct answers for each person, the code determines the highest number of correct answers using the max function on the person list. It then iterates over the person list again to find the person(s) with the highest count and appends their indexes plus one to the answer list.
Finally, the answer list, containing the person(s) with the highest number of correct answers, is returned as the output of the function.
'Coding > Basic Skills(Python)' 카테고리의 다른 글
[Programmers, Level 1] Caesar cipher (0) | 2023.08.21 |
---|---|
[Programmers, Level 1] 푸드 파이트 대회 Food Fight Contest (0) | 2023.07.16 |
[Programmers, Level 1] Babbling 옹알이(2) (0) | 2023.06.25 |
[Programmers, Level 1] Cooking Hamburger 햄버거 만들기 (0) | 2023.06.22 |
[2차원 배열 / 문자열 / for문] 복습 (0) | 2023.06.11 |