Programmers/Lv. 0 (完)

[Kotlin] Programmers 코딩테스트 입문 Day 14 조건문, 반복문, 시뮬레이션, 문자열

chattymin 2023. 3. 25. 08:52
728x90
반응형

가까운 수

import kotlin.math.absoluteValue

class Solution {
    fun solution(array: IntArray, n: Int) = array.sorted().minWithOrNull(compareBy { (it - n).absoluteValue })!!
}

 

 

 

369게임

class Solution {
    fun solution(order: Int) = order.toString().count { it == '3' || it == '6' || it == '9' }
}

 

 

 

암호 해독

class Solution {
    fun solution(cipher: String, code: Int): String {
        var answer: String = ""
        for (i in code-1 until cipher.length step(code))
            answer += cipher[i]
        return answer
    }
}

 

 

 

대문자와 소문자

class Solution {
    fun solution(my_string: String) = my_string.map {
        if (it.isLowerCase()) it.uppercaseChar()
        else it.lowercaseChar()
    }.joinToString("")
}
728x90
반응형