모의고사 - Python

2023. 12. 8. 00:28공부/📝 프로그래머스

1. 풀이 코드

def solution(answers):
    answer = []
    
    one_score = 0
    two_score = 0
    three_score = 0
    
    one_list = [1, 2, 3, 4, 5]
    two_list = [2, 1, 2, 3, 2, 4, 2, 5]
    three_list = [3, 3, 1, 1, 2, 2, 4, 4, 5, 5]
    
    while len(answers) != 0:
        temp_num = answers.pop(0)
        temp_one = one_list.pop(0)
        temp_two = two_list.pop(0)
        temp_three = three_list.pop(0)
        if temp_num == temp_one:
            one_score += 1
        if temp_num == temp_two:
            two_score += 1
        if temp_num == temp_three:
            three_score += 1
        one_list.append(temp_one)
        two_list.append(temp_two)
        three_list.append(temp_three)
        
    if max(one_score, two_score, three_score) == one_score:
        answer.append(1)
    if max(one_score, two_score, three_score) == two_score:
        answer.append(2)
    if max(one_score, two_score, three_score) == three_score:
        answer.append(3)
    return answer

  깊게 생각하지 않고 차근차근 풀었습니다.

 

2. 다른 사람 풀이 코드

def solution(answers):
    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]
    score = [0, 0, 0]
    result = []

    for idx, answer in enumerate(answers):
        if answer == pattern1[idx%len(pattern1)]:
            score[0] += 1
        if answer == pattern2[idx%len(pattern2)]:
            score[1] += 1
        if answer == pattern3[idx%len(pattern3)]:
            score[2] += 1

    for idx, s in enumerate(score):
        if s == max(score):
            result.append(idx+1)

    return result

 

  enumerate()를 정말 잘 썼네요. 본받아야겠습니다.


 

프로그래머스

코드 중심의 개발자 채용. 스택 기반의 포지션 매칭. 프로그래머스의 개발자 맞춤형 프로필을 등록하고, 나와 기술 궁합이 잘 맞는 기업들을 매칭 받으세요.

programmers.co.kr

 

'공부 > 📝 프로그래머스' 카테고리의 다른 글

소수 찾기 - Python  (0) 2023.12.10
소수 만들기 - Python  (0) 2023.12.09
과일 장수 - Python  (0) 2023.12.07
폰켓몬 - Python  (0) 2023.12.06
2016년 - Python  (0) 2023.11.21