ABOUT ME

-

Today
-
Yesterday
-
Total
-
  • [2022 KAKAO TECH INTERNSHIP] 성격 유형 검사하기
    코딩 테스트/Level 1 2022. 8. 20. 22:21
    반응형

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

     

    파이썬

    def solution(survey, choices):
        d = {}
        for s, c in zip(survey, choices):
            if ord(s[0]) < ord(s[1]):
                d[s] = d.get(s, 0) + (c - 4)
            else:
                d[s[::-1]] = d.get(s[::-1], 0) - (c - 4)
        return ''.join((check('RT', d), check('CF', d), check('JM', d), check('AN', d)))
    
    
    def check(type_, d):
        return type_[0] if d.get(type_, 0) <= 0 else type_[1]

     

    Java

    import java.util.HashMap;
    
    class Solution {
        public String solution(String[] survey, int[] choices) {
            var d = new HashMap<Character, Integer>() {{
                put('R', 0);
                put('T', 0);
                put('C', 0);
                put('F', 0);
                put('J', 0);
                put('M', 0);
                put('A', 0);
                put('N', 0);
            }};
            for (var i = 0; i < survey.length; i++) {
                var choice = choices[i] - 4;
                if (choice < 0)
                    d.put(survey[i].charAt(0), d.get(survey[i].charAt(0)) - choice);
                else
                    d.put(survey[i].charAt(1), d.get(survey[i].charAt(1)) + choice);
            }
            return String.valueOf(new char[]{
                    check('R', d.get('R'), 'T', d.get('T')),
                    check('C', d.get('C'), 'F', d.get('F')),
                    check('J', d.get('J'), 'M', d.get('M')),
                    check('A', d.get('A'), 'N', d.get('N')),
            });
        }
    
        char check(char type1, int score1, char type2, int score2) {
            if (score1 > score2) 
                return type1;
            else if (score1 < score2) 
                return type2;
            else 
                return type1 < type2 ? type1 : type2;
        }
    }

    코틀린

    class Solution {
        fun solution(survey: Array<String>, choices: IntArray): String {
            val d = mutableMapOf<String, Int>()
            for ((i, type) in survey.withIndex()) {
                if (type[0] < type[1]) {
                    d[type] = (d[type] ?: 0) + choices[i] - 4
                } else {
                    val key = type.slice(1 downTo 0)
                    d[key] = (d[key] ?: 0) - choices[i] + 4
                }
            }
            return buildString {
                append(check("RT", d))
                append(check("CF", d))
                append(check("JM", d))
                append(check("AN", d))
            }
        }
    
        private fun check(type: String, d: Map<String, Int>) = if ((d[type] ?: 0) > 0) type[1] else type[0]
    }
    반응형
Designed by Tistory.