https://school.programmers.co.kr/learn/courses/30/lessons/118666
문제 설명이 너무 길어서 따로 캡쳐해서 올리진 않겠다.
저번에 배운 defaultdict를 사용한 문제이다.
문제 이해만 하고, defaultdict를 사용해야겠다는 아이디어가 금방 떠올라서 어렵지 않게 문제를 풀 수 있었다.
from collections import defaultdict
def solution(survey, choices):
answer = ''
record = defaultdict(int)
for i in range(len(survey)):
if choices[i] == 1:
record[survey[i][0]] += 3
elif choices[i] == 2:
record[survey[i][0]] += 2
elif choices[i] == 3:
record[survey[i][0]] += 1
elif choices[i] == 4:
continue
elif choices[i] == 5:
record[survey[i][1]] += 1
elif choices[i] == 6:
record[survey[i][1]] += 2
elif choices[i] == 7:
record[survey[i][1]] += 3
print(record)
# defaultdict(<class 'int'>, {'N': 1, 'C': 1, 'M': 2, 'T': 3, 'A': 1})
if max(record['R'], record['T']) == record['R']:
answer += 'R'
else: answer += 'T'
if max(record['C'], record['F']) == record['C']:
answer += 'C'
else: answer += 'F'
if max(record['J'], record['M']) == record['J']:
answer += 'J'
else: answer += 'M'
if max(record['A'], record['N']) == record['A']:
answer += 'A'
else: answer += 'N'
return answer