Kotlin/코딩테스트

[프로그래머스] 성격 유형 검사하기 - kotlin

깨노비 2023. 4. 4. 23:12
728x90
반응형

나만의 카카오 성격 유형 검사지를 만들려고 합니다.
성격 유형 검사는 다음과 같은 4개 지표로 성격 유형을 구분합니다. 성격은 각 지표에서 두 유형 중 하나로 결정됩니다.

class Solution {
    fun solution(survey: Array<String>, choices: IntArray): String {
        val mutableMap = mutableMapOf(
            "R" to 0,
            "T" to 0,
            "C" to 0,
            "F" to 0,
            "J" to 0,
            "M" to 0,
            "A" to 0,
            "N" to 0)

        for ((idx, value) in choices.withIndex()) {
            when (value) {
                1,2,3 -> {
                    val key = survey[idx][0].toString()
                    mutableMap[key] = mutableMap[key]!! + (4 - value)
                }
                5,6,7 -> {
                    val key = survey[idx][1].toString()
                    mutableMap[key] = mutableMap[key]!! + (value - 4)
                }
            }
        }

        val sb = StringBuilder()
        val answer: String = with(sb) {
            append(if (mutableMap["R"]!! < mutableMap["T"]!!) "T" else "R")
            append(if (mutableMap["C"]!! < mutableMap["F"]!!) "F" else "C")
            append(if (mutableMap["J"]!! < mutableMap["M"]!!) "M" else "J")
            append(if (mutableMap["A"]!! < mutableMap["N"]!!) "N" else "A")
        }.toString()

        return answer
    }
}
728x90
반응형