模擬試験問題


私が提出したコード

def solution(answers):
    ans = [0,0,0]
    p = {1: [1,2,3,4,5], 2:[2,1,2,3,2,4,2,5], 3:[3,3,1,1,2,2,4,4,5,5]}
    for a in range(len(answers)-1, -1, -1):
        if answers[a] == p[1][a % 5]:
            ans[0] += 1
        if answers[a] == p[2][a % 8]:
            ans[1] += 1
        if answers[a] == p[3][a % 10]:
            ans[2] += 1
    _max = max(ans)
    return sorted([i+1 for i in range(len(ans)) if ans[i] == _max])

他の人のコードに勉強するところがあるコード

from itertools import cycle # 이런게 있는줄도 몰랐다..! 역시 파이썬이다

def solution(answers):
    giveups = [
        cycle([1,2,3,4,5]),
        cycle([2,1,2,3,2,4,2,5]),
        cycle([3,3,1,1,2,2,4,4,5,5]),
    ]
    scores = [0, 0, 0]
    for num in answers:
        for i in range(3):
            if next(giveups[i]) == num:
                scores[i] += 1
    highest = max(scores)

    return [i + 1 for i, v in enumerate(scores) if v == highest]