본문 바로가기

Development/CodingTest

[2022 KAKAO TECH INTERNSHIP] 성격 유형 검사하기 Python 풀이

https://school.programmers.co.kr/learn/courses/30/lessons/118666

 

프로그래머스

코드 중심의 개발자 채용. 스택 기반의 포지션 매칭. 프로그래머스의 개발자 맞춤형 프로필을 등록하고, 나와 기술 궁합이 잘 맞는 기업들을 매칭 받으세요.

programmers.co.kr

문제 접근

  • 각 성격 유형 별로 score를 저장하는 자료구조가 필요할 듯 근데 default 값이 있어야 성격을 모두 출력 가능하니, defaultdict 사용
  • "RT"던 "TR"이던 신경 쓸 필요는 없어 보임 어차피 score에 따라 점수 부여가 달라지니깐
  • 같은 점수일 경우 사전식으로 정렬하여 출력하면 되니 sort 후 점수 비교하여 answer에 추가

코드

import collections

def solution(survey, choices):
    answer = ''
    types = ["RT", "CF", "JM", "AN"]
    response_table = collections.defaultdict(int)
    
    for i in range(len(survey)):
        score = choices[i] 
        if 1 <= score <= 4:
            response_table[survey[i][0]] += 4 - score

        elif 5 <= score <= 7:
            response_table[survey[i][1]] += score - 4
            
    for type in types:
        type1, type2 = sorted(type)
        answer += type1 if response_table[type1] >= response_table[type2] else type2
        
    return answer