Programmers/Lv. 0 (完)

[Kotlin] Programmers 코딩 기초 트레이닝 Day 08 조건문, 문자열

chattymin 2023. 7. 6. 11:14
728x90
반응형

간단한 논리 연산

class Solution {
    fun solution(x1: Boolean, x2: Boolean, x3: Boolean, x4: Boolean): Boolean = (x1 || x2) && (x3 || x4)
}

 

 

주사위 게임 3

import java.lang.Math.abs

class Solution {
    fun solution(a: Int, b: Int, c: Int, d: Int): Int {
        var temp: Set<Int> = setOf(a,b,c,d)
        var arr = arrayOf(a,b,c,d)

        return when(temp.size){
            1 -> 1111 * temp.first()
            2 -> {
                var first = temp.first()
                var last = temp.last()
                if (arr.count{it == first} == 3){
                    (10 * first + last) * (10 * first + last)
                }else if(arr.count{it == temp.first()} == 2){
                    (first + last) * abs(first - last)
                }else{
                    (10 * last + first) * (10 * last + first)
                }
            }
            3 -> {
                var list = temp.toList()
                val first = list[0]
                val seccond = list[1]
                val third = list[2]

                if (arr.count{it == first} == 2) seccond * third
                else if (arr.count{it == seccond} == 2) first * third
                else first * seccond
            }
            else -> temp.sorted().first()
        }
    }
}

 

 

글자 이어 붙여 문자열 만들기

class Solution {
    fun solution(my_string: String, index_list: IntArray): String = index_list.map { my_string[it] }.joinToString ( "" )
}

 

 

9로 나눈 나머지

class Solution {
    fun solution(number: String): Int = number.fold(0) { total, num -> total + num.digitToInt() } % 9
}

 

 

문자열 여러 번 뒤집기

class Solution {
    fun solution(my_string: String, queries: Array<IntArray>): String {
        var answer: String = my_string

        queries.forEach {
            val str = answer.slice(it.first() .. it.last())
            answer = answer.slice(0 until it.first()) + str.reversed() + answer.slice(it.last() + 1 until my_string.length)
        }

        return answer
    }
}
728x90
반응형