Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete the code to create a sequence from a list.
Kotlin
val numbers = listOf(1, 2, 3, 4, 5) val seq = numbers.[1]()
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'toList()' which returns a list, not a sequence.
Using 'map()' or 'filter()' directly without converting to sequence first.
✗ Incorrect
The asSequence() function converts a collection to a sequence for lazy operations.
2fill in blank
mediumComplete the code to get the first element greater than 3 from a sequence.
Kotlin
val seq = sequenceOf(1, 2, 3, 4, 5) val first = seq.filter { it > 3 }.[1]()
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'last()' which returns the last element.
Using 'find()' which returns nullable and requires a predicate.
✗ Incorrect
The first() function returns the first element matching the condition.
3fill in blank
hardFix the error in the code to sum numbers lazily using a sequence.
Kotlin
val numbers = listOf(1, 2, 3, 4, 5) val sum = numbers.[1]().map { it * 2 }.sum()
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'toList()' which does not enable lazy evaluation.
Using 'map()' directly without converting to sequence.
✗ Incorrect
Using asSequence() allows lazy evaluation before summing.
4fill in blank
hardFill both blanks to create a sequence and filter even numbers.
Kotlin
val numbers = listOf(1, 2, 3, 4, 5) val evens = numbers.[1]().[2] { it % 2 == 0 }.toList()
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'toList()' before filtering which is eager evaluation.
Using 'map' instead of 'filter' to select elements.
✗ Incorrect
First convert to sequence with asSequence(), then filter even numbers with filter.
5fill in blank
hardFill all three blanks to create a sequence, map values, and find the max.
Kotlin
val numbers = listOf(1, 2, 3, 4, 5) val maxVal = numbers.[1]().[2] { it * 3 }.[3]()
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'filter' instead of 'map' for transformation.
Using 'maxBy' which requires a selector function.
Not converting to sequence first.
✗ Incorrect
Convert to sequence with asSequence(), map each value by multiplying by 3, then find the max with max().