[프로그래머스] 성격 유형 검사하기 (파이썬)

철웅·2022년 11월 17일
0

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

💻 Code

def solution(survey, choices):
    
    types = dict(
        R = 0, T = 0,
        C = 0, F = 0,
        J = 0, M = 0,
        A = 0, N = 0
    )

    # 점수 계산
    for i in range(len(survey)):
        if(choices[i] == 1):
            types[survey[i][0]] += 3
        elif(choices[i] == 2):
            types[survey[i][0]] += 2
        elif(choices[i] == 3):
            types[survey[i][0]] += 1
        elif(choices[i] == 4):
            continue
        elif(choices[i] == 5):
            types[survey[i][1]] += 1
        elif(choices[i] == 6):
            types[survey[i][1]] += 2
        elif(choices[i] == 7):
            types[survey[i][1]] += 3
        
    answer = ""
    answer += "R" if types['R'] >= types['T'] else "T"
    answer += "C" if types['C'] >= types['F'] else "F"
    answer += "J" if types['J'] >= types['M'] else "M"
    answer += "A" if types['A'] >= types['N'] else "N"

    return answer
  • chocices[i][0], choices[i][1]을 변수로 할당 해줬으면 더 깔끔했을지도..?


📖 모범 답안

def solution(survey, choices):

    my_dict = {"RT":0,"CF":0,"JM":0,"AN":0}
    for A,B in zip(survey,choices):
        if A not in my_dict.keys():
            A = A[::-1]
            my_dict[A] -= B-4
        else:
            my_dict[A] += B-4

    result = ""
    for name in my_dict.keys():
        if my_dict[name] > 0:
            result += name[1]
        elif my_dict[name] < 0:
            result += name[0]
        else:
            result += sorted(name)[0]

    return result
  • zip() 함수를 활용하였다
  • 점수계산은 저렇게 하는건가보다 (죽었다 깨도 저 생각은 못할거 같다)

0개의 댓글