프로그래머스 코딩 테스트 연습 문제에 있는 완전탐색 - 모의고사 문제이다.
def solution(answers):
cnt = [0, 0, 0]
a=[1,2,3,4,5]
b=[2,1,2,3,2,4,2,5]
c=[3,3,1,1,2,2,4,4,5,5]
for i in range(len(answers)):
if a[i%len(a)] == answers[i]:
cnt[0]+=1
if b[i%len(b)] == answers[i]:
cnt[1]+=1
if c[i%len(c)] == answers[i]:
cnt[2]+=1
answer = []
for idx, score in enumerate(cnt):
if score == max(cnt):
answer.append(idx+1)
return answer
score 배열에서 큰값을 찾아주는 것에 고민을 했는데, 다른 사람의 코드를 참고하여 enumerate 함수를 이용하여 구해주었다.
enumerate 함수는 인덱스 번호와 컬렉션의 원소를 tuple 형태로 반환한다.
t = [1,5,7,33,39,52]
for i,v in enumerate(t):
print("index : {}, value: {}".format(i,v))
# 결과
index : 0, value: 1
index : 1, value : 5
.
.
.
index : 5, value : 52