mapIndexed
์์์ ๊ทธ ์์์ ์ธ๋ฑ์ค๋ฅผ ๋์์ ์ฌ์ฉํด์ผ ํ ๋ ์ ์ฉ
fun Ex1(){
val list = listOf("apple", "banana", "cherry")
val result = list.mapIndexed { index, value -> "$index: $value" }
println(result)
}
fun Ex2(){
val numbers = listOf(1, 2, 3)
val sumWithIndex = numbers.mapIndexed { index, value -> value + index }
println(sumWithIndex)
}
fun Ex3(){
val words = listOf("hello", "world", "kotlin", "programming")
val upperCaseEven = words.mapIndexed { index, value -> if (index % 2 == 0) value.uppercase() else value }
println(upperCaseEven)
}
fun Ex4(){
val numbers2 = listOf(5, 10, 15, 20)
val filteredMapped = numbers2.mapIndexedNotNull { index, value -> if (value % 10 == 0) index else null }
println(filteredMapped)
}
[0: apple, 1: banana, 2: cherry]
[1, 3, 5]
[HELLO, world, KOTLIN, programming]
[1, 3]
โ ๊ณต๋ฐฑ์ผ๋ก ํฉํด์ง ๋ฌธ์์ด์ ๊ณต๋ฐฑ ๊ธฐ์ค์ผ๋ก ๋๋ ํ ๊ฐ ๋ฌธ์๋ฅผ ์ชผ๊ฐ๊ธฐ
fun solution(s: String): String {
var answer = ""
val S = s.split(' ')
val m = S.map { value->
value.mapIndexed { index, c ->
if(index % 2 == 0) c.uppercase() else c
}.joinToString("")
}.joinToString(" ")
println(m)
return answer
}