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

level1이고, 스스로 푼 문제다 

이전 카카오 인턴 코테에서 봤던 문제인데, 그때는 못 풀 것 같아서 넘겼는데 풀어보니 생각보다 어렵지는 않았다 

 

주의해야할 점 

dict.keys()는 리스트가 아닌 dict_keys([dsf, dsfsd]) 형태라는 것. 그래서 리스트 형태로 따로 변환해줘야 한다 

count에서 temp[0] 중복으로써서 처음에 풀이 틀림 

비동의 부분은 1을 선택하면 1점, 2를 선택하면 2점, 3을 선택하면 3점을 받는 것이라고 생각했는데 틀렸다 

문제를 자세히 보니 1을 선택하면 3점, 2를 선택하면 2점, 3을 선택하면 1점을 받는다. 이걸 일일이 분기문으로 작성해야 하나 싶었는데, 

생각해보니 abs(choices[i] - 4) 하면 모든 숫자에 통용된다는 것을 알고 그렇게 했더니 정답이 떴다 

 

 - 정답 풀이 :

def count(dictionary):
    temp = []
    for i in dictionary.keys():
        temp.append(i)
        
    result = ''
    if dictionary[temp[0]] > dictionary[temp[1]]:
        result += temp[0]
    elif dictionary[temp[0]] < dictionary[temp[1]]:
        result += temp[1]
    else:
        result += temp[0]
    return result
                    
def solution(survey, choices):
    answer = ''
    first = {"R" : 0, "T" : 0}
    second = {"C" : 0, "F" : 0}
    third = {"J" : 0, "M" : 0}
    fourth = {"A" : 0, "N" : 0}
    
    for i in range(len(survey)):
        temp = list(survey[i])
        x,y = temp[0], temp[1]
        num = abs(choices[i] - 4)
        if 1 <= choices[i] <= 3:
            if x in first:
                first[x] += num
            elif x in second:
                second[x] += num
            elif x in third:
                third[x] += num
            elif x in fourth:
                fourth[x] += num
            
        elif choices[i] == 4:
            continue
            
        elif 5 <= choices[i] <= 7:
            if y in first:
                first[y] += num
            elif y in second:
                second[y] += num
            elif y in third:
                third[y] += num
            elif y in fourth:
                fourth[y] += num
                
    answer += count(first)
    answer += count(second)
    answer += count(third)
    answer += count(fourth)
                                    
    return answer

+ Recent posts