[프로그래머스(Programmers)][Java,Python] (Lv1) 성격 유형 검사하기 (2022 KAKAO TECH INTERNSHIP)

728x90

 

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

 

프로그래머스

SW개발자를 위한 평가, 교육, 채용까지 Total Solution을 제공하는 개발자 성장을 위한 베이스캠프

programmers.co.kr

 

1. Java

import java.util.*;
class Solution {
    public String solution(String[] survey, int[] choices) {
        String answer = "";
        char[][] type_info = { {'R','T'},{'C','F'},{'J','M'},{'A','N'} };
        int[][] choices_info = { {0,0},{0,3},{0,2},{0,1},{0,0},{1,1},{1,2},{1,3} };
        Map<Character,Integer> my_type = new HashMap<>();
        for ( int i = 0; i < survey.length; ++i ) {
            char type = survey[i].charAt(choices_info[choices[i]][0]);
            int choice = choices_info[choices[i]][1];
            my_type.put(type, my_type.getOrDefault(type, 0) + choice);
        }
        for ( char[] tt : type_info ) {
            char type = 'Z';
            int choice = -1;
            for ( char t : tt ) {
                int choice_tot = my_type.getOrDefault(t,0);
                if ( choice < choice_tot ) {
                    type = t;
                    choice = choice_tot;
                }
            }
            answer += type;
        }
        return answer;
    }
}

(오늘 알아낸 사실... 글에 중괄호({) 연속으로 붙어있으면 글 내용 안 보임..)

 

2. Python

def solution(survey, choices):
    answer = ''
    type_info = [['R','T'],['C','F'],['J','M'],['A','N']]
    choices_info = [[0,0],[0,3],[0,2],[0,1],[0,0],[1,1],[1,2],[1,3]]
    my_type = {}
    for i in range(len(survey)) :
        type = survey[i][choices_info[choices[i]][0]]
        choice = choices_info[choices[i]][1]
        my_type[type] = my_type.get(type,0) + choice
    for tt in type_info :
        type = ''
        choice = -1
        for t in tt :
            choice_tot = my_type.get(t,0)
            if choice < choice_tot :
                type = t
                choice = choice_tot
        answer += type
    return answer

 

반응형