Programmers/Lv. 0 (完)

[Kotlin] Programmers 코딩 기초 트레이닝 Day 23 조건문 활용

chattymin 2023. 7. 22. 15:43
728x90
반응형

부분 문자열

class Solution {
    fun solution(str1: String, str2: String): Int = if (str2.contains(str1)) 1 else 0
}

 

 

꼬리 문자열

class Solution {
    fun solution(str_list: Array<String>, ex: String): String 
    = str_list.filter { !it.contains(ex) }.joinToString("")
}

 

 

정수 찾기

class Solution {
    fun solution(num_list: IntArray, n: Int): Int = if (num_list.contains(n)) 1 else 0
}

 

 

주사위 게임 1

import kotlin.math.abs
class Solution {
    fun solution(a: Int, b: Int): Int =
        if (checkEven(a) && checkEven(b)) abs(a - b) // 둘다 짝수
        else if (checkEven(a) || checkEven(b)) 2 * (a + b) // 둘중 하나 짝수
        else a * a + b * b // 둘다 홀수

    fun checkEven(num: Int): Boolean = (num % 2 == 0)
}

 

 

날짜 비교하기

class Solution {
    fun solution(date1: IntArray, date2: IntArray): Int {
        if (date1[0] < date2[0])
            return 1
        else if (date1[0] > date2[0])
            return 0

        if (date1[1] < date2[1])
            return 1
        else if (date1[1] > date2[1])
            return 0

        if (date1[2] < date2[2])
            return 1

        return 0
    }
}
728x90
반응형