Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete 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'
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.
✗ Incorrect
The zip function combines two collections into pairs of elements.
2fill in blank
mediumComplete 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'
Attempts:
3 left
💡 Hint
Common Mistakes
Using subtraction or multiplication instead of addition.
Using division which may cause unexpected results.
✗ Incorrect
The lambda adds elements from both lists to create sums.
3fill in blank
hardFix 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'
Attempts:
3 left
💡 Hint
Common Mistakes
Using zipWith which does not exist in Kotlin.
Using combine or pairWith which are invalid.
✗ Incorrect
The correct function to combine two lists into pairs is zip.
4fill in blank
hardFill 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'
Attempts:
3 left
💡 Hint
Common Mistakes
Using '*' or '-' operators which do not concatenate strings.
Using only one '+' and missing the other concatenation.
✗ Incorrect
Use '+' to concatenate strings in Kotlin.
5fill in blank
hardFill 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'
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.
✗ Incorrect
Keys are uppercase strings, values are original numbers, then 1 is added to each value.