728x90
반응형
문제출처: https://school.programmers.co.kr/learn/courses/30/lessons/42840
풀이:
def solution(answers):
one = [1, 2, 3, 4, 5]
two = [2, 1, 2, 3, 2, 4, 2, 5]
three = [3, 3, 1, 1, 2, 2, 4, 4, 5, 5]
res = [0, 0, 0]
result = []
for count, answers in enumerate(answers):
if answers == one[count % len(one)]:
res[0] += 1
if answers == two[count % len(two)]:
res[1] += 1
if answers == three[count % len(three)]:
res[2] += 1
for count, i in enumerate(res):
if i == max(res):
result.append(count+1)
return result
print(solution([1,3,2,4,2,3]))
enumerate 로 카운트를 올려서 찍은 정답들의 길이만큼 나눠서 비교하면된다 .
다른 분 풀이:
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]
cycle 을 쓰면 저 수들이 무한반복 하게 된다
반응형
댓글