0
0
Kotlinprogramming~10 mins

Zip for combining collections in Kotlin - Interactive Code Practice

Choose your learning style9 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to combine two lists into pairs using zip.

Kotlin
val numbers = listOf(1, 2, 3)
val words = listOf("one", "two", "three")
val zipped = numbers.[1](words)
println(zipped)
Drag options to blanks, or click blank then click option'
Azip
Bmap
Cfilter
DflatMap
Attempts:
3 left
💡 Hint
Common Mistakes
Using map instead of zip will not combine two lists.
filter and flatMap do not combine two collections into pairs.
2fill in blank
medium

Complete the code to create a list of sums by zipping two lists and adding their elements.

Kotlin
val a = listOf(1, 2, 3)
val b = listOf(4, 5, 6)
val sums = a.zip(b) { x, y -> x [1] y }
println(sums)
Drag options to blanks, or click blank then click option'
A+
B-
C*
D/
Attempts:
3 left
💡 Hint
Common Mistakes
Using subtraction or multiplication instead of addition.
Using division which may cause unexpected results.
3fill in blank
hard

Fix the error in the code to correctly zip two lists and print pairs.

Kotlin
val list1 = listOf("a", "b", "c")
val list2 = listOf(1, 2, 3)
val pairs = list1.[1](list2)
println(pairs)
Drag options to blanks, or click blank then click option'
AzipWith
Bzip
Ccombine
DpairWith
Attempts:
3 left
💡 Hint
Common Mistakes
Using zipWith which does not exist in Kotlin.
Using combine or pairWith which are invalid.
4fill in blank
hard

Fill both blanks to create a zipped list of strings showing pairs separated by a dash.

Kotlin
val first = listOf("red", "green", "blue")
val second = listOf("apple", "leaf", "sky")
val combined = first.zip(second) { a, b -> a [1] "-" [2] b }
println(combined)
Drag options to blanks, or click blank then click option'
A+
B*
D-
Attempts:
3 left
💡 Hint
Common Mistakes
Using '*' or '-' operators which do not concatenate strings.
Using only one '+' and missing the other concatenation.
5fill in blank
hard

Fill all three blanks to create a zipped map from two lists where keys are uppercase strings and values are their lengths.

Kotlin
val keys = listOf("cat", "dog", "bird")
val values = listOf(3, 4, 5)
val map = keys.zip(values).associate { (k, v) -> [1] to [2] }
val result = map.mapValues { it.value [3] 1 }
println(result)
Drag options to blanks, or click blank then click option'
Ak.uppercase()
Bv
C+
Dk.length
Attempts:
3 left
💡 Hint
Common Mistakes
Using k.length instead of uppercase for keys.
Not adding 1 to values in mapValues.
Mixing up keys and values in the associate lambda.