SW Test/Programmers

[Programmers] Lv1.μ‹ κ·œ 아이디 μΆ”μ²œ(kotlin)

An effort will never betray 😎 2022. 7. 18. 22:43
λ°˜μ‘ν˜•

문제

https://school.programmers.co.kr/learn/courses/30/lessons/72410?language=kotlin

 

 

μ˜ˆμ‹œ

https://school.programmers.co.kr/learn/courses/30/lessons/72410?language=kotlin

 

 

풀이

  • 이 λ¬Έμ œλŠ” λ©”μ†Œλ“œ 체이닝 λ°©μ‹μœΌλ‘œ μ—¬λŸ¬ 번의 λ©”μ†Œλ“œλ₯Ό ν˜ΈμΆœν•˜λ©΄ λΉ λ₯΄κ²Œ ν’€ 수 μžˆμŠ΅λ‹ˆλ‹€. ν•˜μ§€λ§Œ 그만큼 λ‹€μ–‘ν•œ ν•¨μˆ˜μ— λŒ€ν•΄μ„œ μ•Œμ§€ λͺ»ν•˜λ©΄ ν•˜λ‚˜μ”© λ‘œμ§μ„ μž‘μ„±ν•˜λŠ” μˆ˜λ°–μ— μ—†μŠ΅λ‹ˆλ‹€.
  • ν•΄λ‹Ή ν’€μ΄λŠ” λ¬Έμ œμ—μ„œ μš”κ΅¬ν•˜λŠ” λŒ€λ‘œ ν•˜λ‚˜μ”© λ§žμΆ°κ°€λ©΄μ„œ ν’€ 수 μžˆμŠ΅λ‹ˆλ‹€.
class Solution {
    fun solution(new_id: String): String {
        var answer: String = ""
        
        val strbuilder = StringBuilder()
        new_id.lowercase().forEach{
            if(it.isLetter() || it.isDigit() || it in arrayOf('-','_','.')){
                if(!(it == '.' && strbuilder.isNotEmpty() && strbuilder.get(strbuilder.length - 1) == '.')){
                    strbuilder.append(it)
                }
            }
        }
        
        if(strbuilder.get(0) == '.') strbuilder.deleteCharAt(0)
        if(strbuilder.isNotEmpty() && strbuilder.get(strbuilder.length - 1) == '.') strbuilder.deleteCharAt(strbuilder.length -1)
        if(strbuilder.isEmpty()) strbuilder.append('a')
        
        if(strbuilder.length >=16){
            val result = strbuilder.substring(0, 15)
            if(result.get(result.length - 1) == '.') return result.substring(0, 14)
            return result
        }else if(strbuilder.length == 2){
            strbuilder.append(strbuilder.get(strbuilder.length - 1))
        }else if(strbuilder.length == 1){
            strbuilder.append(strbuilder.get(strbuilder.length - 1))
            strbuilder.append(strbuilder.get(strbuilder.length - 1))
        }
        
        return strbuilder.toString()
    }
}

 

  • μ •κ·œμ‹μ„ μ‚¬μš©ν•˜λ©΄ νŽΈν•˜κ² λ‹€λŠ” 생각을 ν–ˆμ§€λ§Œ 방법을 λͺ°λΌ λ³„λ„λ‘œ 곡뢀λ₯Ό ν–ˆμŠ΅λ‹ˆλ‹€. 일반적으둜 μ½”λ”©ν…ŒμŠ€νŠΈμ— ν†΅κ³Όν•˜κΈ° μœ„ν•œ μˆ˜μ€€μœΌλ‘œ μ •κ·œμ‹μ„ λ°°μ›Œμ•Ό ν•˜λŠ”μ§€μ— λŒ€ν•œ μ˜λ¬Έμ€ μžˆμ§€λ§Œ μ•Œλ©΄ 훨씬 λΉ λ₯΄κ²Œ ν’€ 수 μžˆλŠ” 건 μ‚¬μ‹€μž…λ‹ˆλ‹€.
  • μ•„λž˜ μ½”λ“œμ²˜λŸΌ λ©”μ†Œλ“œ 체이닝 기법을 μ‚¬μš©ν•˜λ©΄ 가독성 뿐만 μ•„λ‹ˆλΌ μ½”λ“œλ₯Ό 정말 짧게 κ΅¬ν˜„ν•  수 μžˆμŠ΅λ‹ˆλ‹€. ν•¨μˆ˜ν˜• ν”„λ‘œκ·Έλž˜λ°μ˜ μœ„λŒ€ν•¨μ„ 또 ν•œλ²ˆ λŠλΌλ„€μš”
class Solution {
    fun solution(new_id: String): String = new_id.toLowerCase()
            .filter { it.isLetter() || it.isDigit() || it in arrayOf('-','_','.') }
            .replace("[.]+".toRegex(), ".")
            .removePrefix(".")
            .removeSuffix(".")
            .let { if(it.isEmpty()) "a" else it }
            .let { if(it.length >=16) it.substring(0, 15).removeSuffix(".") else it }
            .let {
                if(it.length == 1) it + it + it
                else if(it.length == 2) it + it.last()
                else it
            }
}

 

 

μ°Έκ³ 

 

replace - Kotlin Programming Language

 

kotlinlang.org

 

removePrefix - Kotlin Programming Language

 

kotlinlang.org

 

removeSuffix - Kotlin Programming Language

 

kotlinlang.org

 

substring - Kotlin Programming Language

 

kotlinlang.org

 

λ°˜μ‘ν˜•