[Kotlin] ์ด์ƒํ•œ ๋ฌธ์ž ๋งŒ๋“ค๊ธฐ (ํ”„๋กœ๊ทธ๋ž˜๋จธ์Šค ์•Œ๊ณ ๋ฆฌ์ฆ˜) : split(), mapIndexed(), toUpperCase(), toLowerCase()
๋ฐ˜์‘ํ˜•

 

 

 

 

 

๋ฌธ์ œ

https://school.programmers.co.kr/learn/courses/30/lessons/12930

 

 

 


 

์ ‘๊ทผ

 

  • 1. ๊ณต๋ฐฑ์„ ๊ธฐ์ค€์œผ๋กœ ๋‹จ์–ด๋ฅผ ๋‚˜๋ˆ„์–ด ์ €์žฅํ•œ๋‹ค. : split(" ").map { it }
  • 2. ๋‚˜๋ˆ„์–ด์ง„ ๋‹จ์–ด๋งˆ๋‹ค ์ง์ˆ˜ ์ธ๋ฑ์Šค๋Š” ๋Œ€๋ฌธ์ž๋กœ ๋ณ€ํ™˜ํ•˜๋Š” ์กฐ๊ฑด์‹์„ ์ž‘์„ฑํ•œ๋‹ค. : toUpperCase(), toLowerCase()
  • 3. ๋ณ€ํ™˜๋œ ๋‹จ์–ด๋ฅผ ๋‹ค์‹œ ๊ณต๋ฐฑ์„ ๋„ฃ์–ด ํ•ฉ์ณ ๋ฆฌํ„ดํ•œ๋‹ค.
  • ์—ฌ๊ธฐ์„œ์˜ ์˜๋ฌธ์ ์€ 1๋ฒˆ์—์„œ ๋‚˜๋ˆ„์–ด์งˆ ๋‹จ์–ด๊ฐ€ ์ด ๋ช‡ ๊ฐœ์ธ์ค„ ์•Œ๊ณ  ๋ฐฐ์—ด์„ ๋ฏธ๋ฆฌ ์„ ์–ธํ•œ๋‹จ ๋ง์ธ๊ฐ€?
    ๊ทธ๋ฆฌ๊ณ  3๋ฒˆ์—์„œ ๋‹ค์‹œ ๊ทธ ๊ฐœ์ˆ˜๋งŒํผ ๊ณต๋ฐฑ์„ ๋„ฃ์–ด ์–ด๋–ป๊ฒŒ ํ•ฉ์นœ๋‹จ ๋ง์ธ๊ฐ€?

 

 


 

์„ฑ๊ณต ์ฝ”๋“œ

 

class Solution {
    fun solution(s: String): String =
        s.split(" ").joinToString(" ") {
            it.mapIndexed { i, c ->
                when (i % 2) {
                    0 -> c.toUpperCase()
                    else -> c.toLowerCase()
                }
            }.joinToString("")
        }
}

 


 

์„ค๋ช…

 

  1. s.split(" ").joinToString(" ")
    - split()์„ ์ด์šฉํ•˜์—ฌ ๊ณต๋ฐฑ ๋‹จ์œ„๋กœ ๋‹จ์–ด๋ฅผ ๋Š์–ด์„œ ๊ฐ ๋ฐฐ์—ด์— ์ €์žฅํ•œ๋‹ค.
    - joinToString์œผ๋กœ split์ด ๋งŒ๋“  ๋ฐฐ์—ด์„ ๋ฌธ์ž์—ด๋กœ ๋ฐ”๊พธ์–ด์ค€๋‹ค.
    - joinToString() ๊ด„ํ˜ธ ๋ถ€๋ถ„์€ separator ๋กœ ์—ฌ๊ธฐ์„œ ๊ตฌ๋ถ„์ž๋ฅผ ์ •ํ•ด์ค„ ์ˆ˜ ์žˆ๋‹ค.
  2. it.mapIndexed { i, c -> ... }.joinToString("")
    - 1๋กœ ๊ตฌ๋ถ„๋œ ๊ฐ๊ฐ์˜ ๋ฐฐ์—ด(it) ๋งˆ๋‹ค mapIndexed๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ, ์ง์ˆ˜์ธ๋ฑ์Šค๋Š” ๋Œ€๋ฌธ์ž, ํ™€์ˆ˜์ธ๋ฑ์Šค๋Š” ์†Œ๋ฌธ์ž๋กœ ๋งŒ๋“ ๋‹ค.
    - mapIndexed ํ•จ์ˆ˜๋Š” ๋ฌธ์ž์—ด์˜ ๊ฐ ๋ฌธ์ž์— ๋Œ€ํ•ด ์ธ๋ฑ์Šค์™€ ๋ฌธ์ž๋ฅผ ์ž…๋ ฅ์œผ๋กœ ๋ฐ›์•„ ํ•จ์ˆ˜๋ฅผ ์ ์šฉํ•˜๊ณ , ๊ทธ ๊ฒฐ๊ณผ๋ฅผ ์ƒˆ๋กœ์šด ๋ฆฌ์ŠคํŠธ๋กœ ๋ฐ˜ํ™˜ํ•˜๋ฏ€๋กœ ๋‹ค์‹œ joinToString("") ์œผ๋กœ ๋‹ค์‹œ ์ด์–ด์ค€๋‹ค.

 

 

 

๐Ÿ’กsplit

  • ๊ด„ํ˜ธ์•ˆ์˜ ๊ตฌ๋ถ„์ž๋ฅผ ๊ธฐ์ค€์œผ๋กœ ๋ฌธ์ž์—ด์„ ๋ถ„๋ฆฌํ•˜์—ฌ ๋ฐฐ์—ด๋กœ ๋ฐ˜ํ™˜ํ•˜๋Š” ํ•จ์ˆ˜
  • ์ฃผ์–ด์ง„ ๋ฌธ์ž์—ด์„ ํŠน์ • ๊ตฌ๋ถ„์ž๋ฅผ ๊ธฐ์ค€์œผ๋กœ ๋‚˜๋ˆ„๊ณ  ๊ฐ๊ฐ์˜ ๋ถ€๋ถ„์„ ๋ฐฐ์—ด์— ์ €์žฅํ•˜๋Š” ๊ฒƒ์ด๋ผ๊ณ  ํ•  ์ˆ˜ ์žˆ์Œ
val str = "apple banana orange"
val parts = str.split(" ")
println(parts) // [apple, banana, orange]
  • ์œ„ ์˜ˆ์‹œ์—์„œ๋Š” str ๋ฌธ์ž์—ด์„ ๊ณต๋ฐฑ์„ ๊ธฐ์ค€์œผ๋กœ ๋‚˜๋ˆ„์–ด parts ๋ฐฐ์—ด์— ์ €์žฅํ•จ
  • ๊ฒฐ๊ณผ์ ์œผ๋กœ parts ๋ฐฐ์—ด์—๋Š” "apple", "banana", "orange" ์„ธ ๊ฐœ์˜ ๋ฌธ์ž์—ด์ด ๋“ค์–ด์žˆ์Œ
val str = "apple,banana,orange,grape"
val fruits = str.split(",", limit = 2)
println(fruits) // [apple, banana, orange, grape]
  • split ํ•จ์ˆ˜์˜ ๋‘ ๋ฒˆ์งธ ์ธ์ž๋กœ๋Š” ์ œํ•œ๋œ ๊ฐœ์ˆ˜์˜ ๋ถ„๋ฆฌ๋ฅผ ์ง€์ •ํ•  ์ˆ˜ ์žˆ์œผ๋ฉฐ, ์ด๋ฅผ ํ†ตํ•ด ๋‚˜๋ˆŒ ๋ฌธ์ž์—ด์˜ ์ตœ๋Œ€ ๊ฐœ์ˆ˜๋ฅผ ์ง€์ •
  • ์œ„ ์˜ˆ์‹œ์—์„œ๋Š” limit ๋งค๊ฐœ๋ณ€์ˆ˜๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ์ตœ๋Œ€ ๋‘ ๊ฐœ์˜ ๋ถ€๋ถ„์œผ๋กœ ๋ฌธ์ž์—ด์„ ๋ถ„๋ฆฌ
  • ๊ฒฐ๊ณผ์ ์œผ๋กœ fruits ๋ฐฐ์—ด์—๋Š” "apple", "banana,orange,grape" ๋‘ ๊ฐœ์˜ ๋ฌธ์ž์—ด์ด ๋“ค์–ด์žˆ์Œ

 

 

 

๐Ÿ’กtoLowerCase(), toUpperCase()

val str = "Hello, World!"

val lowerCaseStr = str.toLowerCase()
println(lowerCaseStr) // ์ถœ๋ ฅ: hello, world!

val upperCaseStr = str.toUpperCase()
println(upperCaseStr) // ์ถœ๋ ฅ: HELLO, WORLD!
  • ๋ฌธ์ž์—ด์˜ ๋Œ€์†Œ๋ฌธ์ž๋ฅผ ๋ณ€ํ™˜
  • ์ด ๋ฉ”์„œ๋“œ๋“ค์€ ๋ฌธ์ž์—ด์„ ๋ณ€๊ฒฝํ•˜์ง€ ์•Š๊ณ  ์ƒˆ๋กœ์šด ๋ฌธ์ž์—ด์„ ๋ฐ˜ํ™˜ํ•˜๊ธฐ ๋•Œ๋ฌธ์—, ์›๋ณธ ๋ฌธ์ž์—ด์€ ๊ทธ๋Œ€๋กœ ์œ ์ง€๋จ

 

 

 

 

๐Ÿ’กmapIndexed()

fun <T, R> Iterable<T>.mapIndexed(transform: (index: Int, T) -> R): List<R>
val list = listOf("apple", "banana", "orange")

// ๊ฐ ์š”์†Œ์™€ ์ธ๋ฑ์Šค๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ๋ฌธ์ž์—ด์˜ ๊ธธ์ด์™€ ํ•จ๊ป˜ ๋ฌธ์ž์—ด์„ ๋ณ€ํ™˜
val result = list.mapIndexed { index, element ->
    "$index: ${element.toUpperCase()}(${element.length})"
}

println(result) // ์ถœ๋ ฅ: [0: APPLE(5), 1: BANANA(6), 2: ORANGE(6)]
  • ์ปฌ๋ ‰์…˜์˜ ๊ฐ ์š”์†Œ์— ๋Œ€ํ•ด ์ธ๋ฑ์Šค์™€ ํ•จ๊ป˜ ์ž‘์—…ํ•  ์ˆ˜ ์žˆ๋„๋ก ํ•ด์ฃผ๋Š” ํ•จ์ˆ˜
  • ๊ฐ ์š”์†Œ์— ๋Œ€ํ•ด ์ธ๋ฑ์Šค๋ฅผ ํฌํ•จํ•˜์—ฌ ์ง€์ •๋œ ๋ณ€ํ™˜ ํ•จ์ˆ˜๋ฅผ ์ ์šฉํ•  ์ˆ˜ ์žˆ์Œ
  • ๋งค๊ฐœ๋ณ€์ˆ˜(transfrom): ์š”์†Œ์™€ ์ธ๋ฑ์Šค์— ๋Œ€ํ•ด ์ ์šฉํ•  ๋ณ€ํ™˜ ํ•จ์ˆ˜. ์ธ๋ฑ์Šค์™€ ์š”์†Œ๋ฅผ ๋งค๊ฐœ๋ณ€์ˆ˜๋กœ ๋ฐ›๊ณ  ๋ณ€ํ™˜๋œ ๊ฒฐ๊ณผ๋ฅผ ๋ฐ˜ํ™˜
  • ๋ฐ˜ํ™˜๊ฐ’: ๋ณ€ํ™˜๋œ ์š”์†Œ๋กœ ์ด๋ฃจ์–ด์ง„ ์ƒˆ๋กœ์šด ๋ฆฌ์ŠคํŠธ์— ์ €์žฅ๋˜์–ด ๋ฐ˜ํ™˜

 

 

 


 

๋‹ค๋ฅธ ๋ฐฉ๋ฒ•

 

class Solution {
    fun solution(s: String): String {
        var answer = ""
        var idxInWord = 0

        for (c in s) {
            if (c==' ') {
                answer += " "
                idxInWord = 0
            } else {
                idxInWord++
                if (idxInWord%2 != 0) {
                    answer += c.toUpperCase()
                } else {
                    answer += c.toLowerCase()
                }
            }
        }

        return answer
    }
}
  • mapIndexed() ๋“ฑ์˜ ๊ฐœ๋…์ด ์—†์„ ๋•Œ, ๋‚ด๊ฐ€ ๊ตฌ์ƒํ–ˆ๋˜ ์•Œ๊ณ ๋ฆฌ์ฆ˜๊ณผ ๊ฐ™์ด ํ’€์ดํ•˜๋Š” ๋ฐฉ์‹์ด๋‹ค.
  • for๋ฌธ์œผ๋กœ (c in s) ์กฐ๊ฑด์„ ์ž…๋ ฅํ•˜์—ฌ s๊ฐ€ ์–ด๋Š ๊ธธ์ด์˜ ๋‹จ์–ด์ธ์ง€ ์•Œ ์ˆ˜ ์—†์–ด๋„ ๊ตฌํ˜„ํ•  ์ˆ˜ ์žˆ๋‹ค.
    ์ €๋ ‡๊ฒŒ ๊ตฌํ˜„ํ•˜๋ฉด ์ŠคํŠธ๋ง s ์•ˆ์— ์žˆ๋Š” char ํ•˜๋‚˜ํ•˜๋‚˜๊ฐ€ ๊บผ๋‚ด์ง„๋‹ค.

 

 

 

 

 

 

๋ฐ˜์‘ํ˜•
 ๐Ÿ’ฌ C O M M E N T