1번 문제부터 마지막 문제까지의 정답이 순서대로 들은 배열 answers가 주어졌을 때, 가장 많은 문제를 맞힌 사람이 누구인지 배열에 담아 반환하는 함수 구현하기
수포자는 수학을 포기한 사람의 준말입니다. 수포자 삼인방은 모의고사에 수학 문제를 전부 찍으려 합니다. 수포자는 1번 문제부터 마지막 문제까지 다음과 같이 찍습니다.
1번 수포자가 찍는 방식: 1, 2, 3, 4, 5, 1, 2, 3, 4, 5, ... 2번 수포자가 찍는 방식: 2, 1, 2, 3, 2, 4, 2, 5, 2, 1, 2, 3, 2, 4, 2, 5, ... 3번 수포자가 찍는 방식: 3, 3, 1, 1, 2, 2, 4, 4, 5, 5, 3, 3, 1, 1, 2, 2, 4, 4, 5, 5, ...
1번 문제부터 마지막 문제까지의 정답이 순서대로 들은 배열 answers가 주어졌을 때, 가장 많은 문제를 맞힌 사람이 누구인지 배열에 담아 return 하도록 solution 함수를 작성해주세요.
answers | return |
[1,2,3,4,5] | [1] |
[1,3,2,4,2] | [1,2,3] |
function solution(answers) {
var answer = [];
const supo1 = [1, 2, 3, 4, 5];
const supo2 = [2, 1, 2, 3, 2, 4, 2, 5];
const supo3 = [3, 3, 1, 1, 2, 2, 4, 4, 5, 5];
let score = [0, 0, 0];
let highScore = 0;
for (let i = 0; i < answers.length; i++) {
if (answers[i] === supo1[i % supo1.length]) score[0]++;
if (answers[i] === supo2[i % supo2.length]) score[1]++;
if (answers[i] === supo3[i % supo3.length]) score[2]++;
}
highScore = Math.max(...score);
for (let i = 0; i < score.length; i++) {
if (highScore === score[i]) answer.push(i + 1);
}
return answer;
}
function solution(answers) {
// Define the answer patterns for each Supoja
const pattern1 = [1, 2, 3, 4, 5];
const pattern2 = [2, 1, 2, 3, 2, 4, 2, 5];
const pattern3 = [3, 3, 1, 1, 2, 2, 4, 4, 5, 5];
// Initialize scores for each Supoja
let score1 = 0, score2 = 0, score3 = 0;
// Calculate scores by comparing answers
answers.forEach((answer, i) => {
if (answer === pattern1[i % pattern1.length]) score1++;
if (answer === pattern2[i % pattern2.length]) score2++;
if (answer === pattern3[i % pattern3.length]) score3++;
});
// Determine the highest score
const maxScore = Math.max(score1, score2, score3);
// Gather all Supojas who achieved the maximum score
let result = [];
if (score1 === maxScore) result.push(1);
if (score2 === maxScore) result.push(2);
if (score3 === maxScore) result.push(3);
return result;
}
// Examples to test the function
console.log(solution([1, 2, 3, 4, 5])); // [1]
console.log(solution([1, 3, 2, 4, 2])); // [1, 2, 3]
def solution(answers):
# Supoja answer patterns
pattern1 = [1, 2, 3, 4, 5]
pattern2 = [2, 1, 2, 3, 2, 4, 2, 5]
pattern3 = [3, 3, 1, 1, 2, 2, 4, 4, 5, 5]
# Lengths of each pattern
len1 = len(pattern1)
len2 = len(pattern2)
len3 = len(pattern3)
# Scores for each Supoja
score1 = 0
score2 = 0
score3 = 0
# Compute scores
for i, correct in enumerate(answers):
if pattern1[i % len1] == correct:
score1 += 1
if pattern2[i % len2] == correct:
score2 += 1
if pattern3[i % len3] == correct:
score3 += 1
# Determine maximum score
max_score = max(score1, score2, score3)
# Collect all Supojas with the maximum score
result = []
if score1 == max_score:
result.append(1)
if score2 == max_score:
result.append(2)
if score3 == max_score:
result.append(3)
return result
def solution(answers):
# Supoja answer patterns
pattern1 = [1, 2, 3, 4, 5]
pattern2 = [2, 1, 2, 3, 2, 4, 2, 5]
pattern3 = [3, 3, 1, 1, 2, 2, 4, 4, 5, 5]
# Lengths of each pattern
len1 = len(pattern1)
len2 = len(pattern2)
len3 = len(pattern3)
# Scores for each Supoja
score1 = 0
score2 = 0
score3 = 0
# Compute scores
for i, correct in enumerate(answers):
if pattern1[i % len1] == correct:
score1 += 1
if pattern2[i % len2] == correct:
score2 += 1
if pattern3[i % len3] == correct:
score3 += 1
# Determine maximum score
max_score = max(score1, score2, score3)
# Collect all Supojas with the maximum score
result = []
if score1 == max_score:
result.append(1)
if score2 == max_score:
result.append(2)
if score3 == max_score:
result.append(3)
return result