JAVAIARY

프로그래머스) 성격유형 검사하기 본문

examplePractice

프로그래머스) 성격유형 검사하기

shiherlis 2023. 3. 21. 17:55

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

 

프로그래머스

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

programmers.co.kr

 

 class Solution {
    public String solution(String[] survey, int[] choices) {
	        String answer = "";
	        // 비동의 - 동의 선택지
	        //RT, CF, MJ, AN 
	        // 동점인 경우 알파벳순 
	        
	        int[] personality = new int[8];
	        // 각각 R, T, C, F, J, M, A, N 의 점수를 담을 배열
	        
	        for (int i = 0; i < survey.length; i++) {

		        // 비동의 선택지 / 동의 선택지 
		        int disagree = applyScore(survey[i].charAt(0));
		        int agree = applyScore(survey[i].charAt(1));
                
		        // 4 : 모르겠음 선택시 점수 변동이 없으므로 skip
		        switch (choices[i]) {
				case 1: 
					personality[disagree] +=3;
					break;
				case 2: 
					personality[disagree] +=2;
					break;
				case 3: 
					personality[disagree] +=1;
					break;
				case 5: 
					personality[agree] +=1;
					break;
				case 6: 
					personality[agree] +=2;
					break;
				case 7: 
					personality[agree] +=3;
					break;
				}
			}
	        answer = personality[0]>=personality[1] ? answer+'R': answer+'T';
	        answer = personality[2]>=personality[3] ? answer+'C': answer+'F';
	        answer = personality[4]>=personality[5] ? answer+'J': answer+'M';
	        answer = personality[6]>=personality[7] ? answer+'A': answer+'N';
	        
	        return answer;
	    }
	// 점수를 더해줄 인덱스 구하는 함수
	public static int applyScore(char s) {
		int idx = 0;
		switch (s) {
		case 'R':
			idx = 0;
			return idx;
		case 'T':
			idx = 1;
			return idx;
		case 'C':
			idx = 2;
			return idx;
		case 'F':
			idx = 3;
			return idx;
		case 'J':
			idx = 4;
			return idx;
		case 'M':
			idx = 5;
			return idx;
		case 'A':
			idx = 6;
			return idx;
		case 'N':
			idx = 7;
			return idx;
		}
		return idx;
	}
}
  • 그냥 문제 그대로 구현해버렸음
  • 다른사람의 풀이를 보니 점수를 한번에 구현하는 게 핵심이었던 것 같다.
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;
    }
}

 

  • 안쓰는 자료형은 까먹게 된다. (ex. hashMap)
  • key, value 가 쌍으로 있어 유형별 점수를 부여해 주기에 딱인 것 같다.