본문 바로가기

개발지/코테

[프로그래머스] 코테연습 - 성격 유형 검사하기 (lv.1)

- 내 방법 풀이

  • 초이스배열을 순회하며, 유형당 점수를 줄 생각을 했다. 점수는 Map을 사용할까 하다가, 배열로 저장하는 방법을 선택했다.
  • survey를 보며, 동의/비동의를 결정하여 점수를 더해주기 위해, checkType으로 리턴한 String의 charAt메서드로 점수를 더해줄 유형을 찾았다.
  • 이후 점수를 비교해서 answer에 더해주는 식.
class Solution {
    public String solution(String[] survey, int[] choices) {
        String answer = "";
        int[] typeScore = new int[8];
        // 초이스 배열 순회, 숫자에 따라 성격유형당 점수 증가.
        // 성격유형은 어떻게? 일반 배열로 하자. {R, T, F, C, M, J, A, N}
        for (int i = 0; i < choices.length; i++) { // 배열 순회
            int check = choices[i];
            if (check == 4) continue;
            else { 
                String type = checkType(survey[i]);
                if (check < 4) {
                    typeScore[type.charAt(0) - '0'] += 4 - check;
                } 
                if (check > 4) {
                    typeScore[type.charAt(1) - '0'] += check - 4;
                }
            }
        }
        
        if (typeScore[0] >= typeScore[1]) answer += "R";
        else answer += "T";
        
        if (typeScore[2] > typeScore[3]) answer += "F";
        else answer += "C";
        
        if (typeScore[4] > typeScore[5]) answer += "M";
        else answer += "J";
        
        if (typeScore[6] >= typeScore[7]) answer += "A";
        else answer += "N";
        
        return answer;
    }
    public String checkType(String type) {
        switch (type) {
            case "RT": return "01";
            case "TR": return "10";
            case "FC": return "23";
            case "CF": return "32";
            case "MJ": return "45";
            case "JM": return "54";
            case "AN": return "67";
            case "NA": return "76";
        }
        return "";
    }
}

 

- 다른 사람 방법 풀이

  • 깔끔한 풀이. 똑같은 로직을 가졌더라도 차이는 여기서 나는 것 같다. 이래서 리팩토링이 중요한 걸까.
  • char[][]배열과 HashMap으로 유형별 점수를 기록했고, 점수를 체크하는 것도 score 배열을 통해서 체크한 문항에 따라 쉽게 해당 인덱스의 숫자를 더하기만 하면 되게 해놓았다.
  • 이후 choice 배열을 순회하면서, 유형별 점수를 더해주고, 마지막에 지표별 점수를 비교하는 과정도 깔끔하다.
import java.util.HashMap;

class Solution {
    public String solution(String[] survey, int[] choices) {
        String answer = "";
        char [][] type = {{'R', 'T'}, {'C', 'F'}, {'J', 'M'}, {'A', 'N'}};
        int [] score = {0, 3, 2, 1, 0, 1, 2, 3};
        HashMap<Character, Integer> point = new HashMap<Character, Integer>();

        // 점수 기록할 배열 초기화 
        for (char[] t : type) {
            point.put(t[0], 0);
            point.put(t[1], 0);
        }

        // 점수 기록 
        for (int idx = 0; idx < choices.length; idx++){
            if(choices[idx] > 4){
                point.put(survey[idx].charAt(1), point.get(survey[idx].charAt(1)) + score[choices[idx]]);
            } else {
                point.put(survey[idx].charAt(0), point.get(survey[idx].charAt(0)) + score[choices[idx]]);
            }
        }

        // 지표 별 점수 비교 후 유형 기입
        for (char[] t : type) {
            answer += (point.get(t[1]) <= point.get(t[0])) ? t[0] : t[1];
        }

        return answer;
    }
}