Scribbling

프로그래머스: 모의고사 본문

Computer Science/Coding Test

프로그래머스: 모의고사

focalpoint 2021. 10. 20. 00:50

Generator를 이용한 해법

def solution(answers):
    def gen1():
        candidates = [1, 2, 3, 4, 5]
        while True:
            for candidate in candidates:
                yield candidate
    def gen2():
        candidates = [2, 1, 2, 3, 2, 4, 2, 5]
        while True:
            for candidate in candidates:
                yield candidate
    def gen3():
        candidates = [3, 3, 1, 1, 2, 2, 4, 4, 5, 5]
        while True:
            for candidate in candidates:
                yield candidate
    g1 = gen1()
    g2 = gen2()
    g3 = gen3()
    
    answer = []
    correct = [0] * 3
    for answer in answers:
        if next(g1) == answer:
            correct[0] += 1
        if next(g2) == answer:
            correct[1] += 1
        if next(g3) == answer:
            correct[2] += 1
    
    answer = [i+1 for i in range(3) if correct[i] == max(correct)]
    
    return answer