Sobes.tech
Junior — Middle

How can you convert a list of key-value pairs into a list of numbers in Kotlin, where each number is half of the corresponding value?

sobes.tech AI

Answer from AI

In Kotlin, to convert a list of pairs Pair<K, V> into a list of numbers where each number is half of the corresponding value, you can use the map function. Assuming the values are numeric (for example, Int or Double), an example would be:

val pairs: List<Pair<String, Int>> = listOf("a" to 10, "b" to 20, "c" to 30)
val halfValues: List<Double> = pairs.map { it.second / 2.0 }
println(halfValues) // [5.0, 10.0, 15.0]

Here, it.second is the value from the pair, and dividing by 2.0 results in half of the value as a Double. If the values are already Double, division can be done directly.