TIL #32

loci·2024년 6월 1일
0

TIL

목록 보기
31/103


성격유형 검사하기

설문유형과 답을 비교해서 각 답의 점수를 해당 성격 유형에 추가해 두 유형중 높은 점수를 반환해야한다.
어떻게 풀어야할지 몰라 고민하다가 풀지 못했다.


풀다가 실패한 풀이

class Solution {
    fun solution(survey: Array<String>, choices: IntArray): String {
        var answer: String = ""
        var result = ""
        var array1 = IntArray(4){0}
        var array2 = IntArray(4){0}
        
        
        for(i in choices.indices){
            println(choices[i])
            
            when(choices[i]){
                1 -> result += survey[i][0].toString().repeat(3)
                2 -> result += survey[i][0].toString().repeat(2)
                3 -> result += survey[i][0].toString().repeat(1)
                5 -> result += survey[i][1].toString().repeat(1)
                6 -> result += survey[i][1].toString().repeat(2)
                7 -> result += survey[i][1].toString().repeat(3)
                else -> {}
            }
            
        }
        
        for( i in result){
            when(i.toString()){
                "R" ->  array1[0]++
                "C" ->  array1[1]++
                "J" ->  array1[2]++
                "A" ->  array1[3]++
                "T" ->  array2[0]++
                "F" ->  array2[1]++
                "M" ->  array2[2]++
                else -> array2[3]++
            }
        }
        
        println(result)
        return answer
    }
}

map을 이용했어야 했다.


다른 사람의 풀이

class Solution {
    fun solution(survey: Array<String>, choices: IntArray): String {
        val orders = listOf("RT", "CF", "JM", "AN")
        return choices
            .mapIndexed { index, i ->
                if (i-4 < 0) {
                    survey[index][0] to -(i-4)
                } else {
                    survey[index][1] to (i-4)
                }
            }
            .groupBy { it.first }
            .map { it.key to it.value.sumOf { v:Pair<Char, Int> -> v.second } }
            .toMap()
            .let { ans:Map<Char, Int> ->
                orders.map {
                    if (ans.getOrDefault(it[0], 0) >= ans.getOrDefault(it[1], 0)) it[0] else it[1]
                }
            }
            .joinToString("")
    }
}
class Solution {
fun solution(survey: Array<String>, choices: IntArray): String {
        val scoreMap = mutableMapOf("RT" to 0, "CF" to 0, "JM" to 0, "AN" to 0)

        survey.forEachIndexed { index, key ->
            if (scoreMap.keys.contains(key)) {
                scoreMap[key] = scoreMap[key]!! + choices[index] - 4
            } else {
                scoreMap[key.reversed()] = scoreMap[key.reversed()]!! - (choices[index] - 4)
            }
        }

        var answer = ""
        scoreMap.forEach { (key, value) -> answer += if(value > 0) key[1] else key[0] }
        return answer
    }
}
class Solution {
    fun solution(survey: Array<String>, choices: IntArray): String {
        // 각 성격 유형의 점수를 저장할 맵
        val scores = 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 (i in survey.indices) {
            val (disagree, agree) = survey[i][0] to survey[i][1]
            when (choices[i]) {
                1 -> scores[disagree] = scores[disagree]!! + 3
                2 -> scores[disagree] = scores[disagree]!! + 2
                3 -> scores[disagree] = scores[disagree]!! + 1
                5 -> scores[agree] = scores[agree]!! + 1
                6 -> scores[agree] = scores[agree]!! + 2
                7 -> scores[agree] = scores[agree]!! + 3
                // choices[i]가 4인 경우는 점수를 추가하지 않음 (모르겠음)
            }
        }
        
        // 각 지표의 성격 유형 결정
        val result = StringBuilder()
        result.append(if (scores['R']!! >= scores['T']!!) 'R' else 'T')
        result.append(if (scores['C']!! >= scores['F']!!) 'C' else 'F')
        result.append(if (scores['J']!! >= scores['M']!!) 'J' else 'M')
        result.append(if (scores['A']!! >= scores['N']!!) 'A' else 'N')
        
        return result.toString()
    }
}
profile
편리한 개발자

0개의 댓글