문제: 주어진 점수를 계산하여 성격유형 검사 결과 출력하기.
풀이 코드:
def solution(survey, choices):
answer = ''
dic1 = {'R': 0, 'T': 0, 'C': 0, 'F': 0, 'J': 0, 'M': 0, 'A': 0, 'N': 0}
lst = 'RTCFJMAN'
for string, score in list(zip(survey, choices)):
if score == 4:
continue
elif score > 4:
dic1[string[1]] += abs(score - 4)
else:
dic1[string[0]] += abs(score - 4)
for i in range(0, len(lst), 2):
if dic1[lst[i]] >= dic1[lst[i + 1]]:
answer += lst[i]
else:
answer += lst[i + 1]
return answer
풀이 설명:
성격 유형에 대한 점수를 확인하기 위해 다음과 같이 탐색을 해주었습니다.
또한, 탐색 결과를 바탕으로 문제 조건에 맞게 점수를 배분하였습니다.
- 4점 이상일 경우, 'RT'(예시)에서 T에 점수를 배정
- 4점 이하일 경우, 'RT'에서 R에 점수를 배정
for string, score in list(zip(survey, choices)):
if score == 4:
continue
elif score > 4:
dic1[string[1]] += abs(score - 4)
else:
dic1[string[0]] += abs(score - 4)
이후, 각 성격 유형에 대해 점수가 다 반영되면 각 카테고리 별로 강한 성향을 보이는 성격 유형을 answer에 담아주었습니다.
for i in range(0, len(lst), 2):
if dic1[lst[i]] >= dic1[lst[i + 1]]:
answer += lst[i]
else:
answer += lst[i + 1]
'알고리즘 > 프로그래머스' 카테고리의 다른 글
[프로그래머스/python] 호텔 대실 (0) | 2023.03.02 |
---|---|
[프로그래머스/python] kakao 캐시 (0) | 2022.10.31 |
[프로그래머스/python] 카카오 문자열 압축 (0) | 2022.10.27 |
[프로그래머스/python] kakao 3차 압축 (0) | 2022.10.25 |
[프로그래머스/python] kakao 1차 뉴스 클러스터링 (0) | 2022.10.12 |