[프로그래머스] 모의고사

SEUNGCHAN BAEK·2021년 4월 1일
0
post-thumbnail


def solution(answers):
    answers = [str(i) for i in answers]
    answer = ''.join(answers)
    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]
    counta1 = 0
    counta2 = 0
    counta3 = 0
    totcount = []

    for i in range(len(answers)):
        if int(answer[i]) == a1[i%5]:
            counta1 += 1
        if int(answer[i]) == a2[i%8]:
            counta2 += 1
        if int(answer[i]) == a3[i%10]:
            counta3 += 1

    totcount.append(counta1)
    totcount.append(counta2)
    totcount.append(counta3)

    final = []

    for i in range(3):
        if totcount[i] == max(totcount):
            final.append(i+1)

    return final

이 문제는 효율성을 별로 중요하게 생각하지 않는거같다. 효율적으로 풀지 못한것 같았는데 정답으로 인정되었다. answers의 길이가 바뀔수 있으니 i%5, i%8, i%10 이렇게 길이만큼 나눈 나머지를 계산했다. 밑에 final를 리턴하는 for문은 좀더 효율적인 코드가 되도록 생각을 더 해봐야 될 것 같다.

profile
백엔드 개발자가 되는 그날까지

0개의 댓글

관련 채용 정보