0
0
Kotlinprogramming~10 mins

Sequence operators (map, filter) 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 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'
Afilter
Bmap
CasSequence
DtoList
Attempts:
3 left
💡 Hint
Common Mistakes
Using map or filter directly on the list without converting to sequence.
2fill in blank
medium

Complete the code to filter even numbers from the sequence.

Kotlin
val numbers = sequenceOf(1, 2, 3, 4, 5)
val evens = numbers.[1] { it % 2 == 0 }
Drag options to blanks, or click blank then click option'
Afilter
BflatMap
Cmap
Dreduce
Attempts:
3 left
💡 Hint
Common Mistakes
Using map instead of filter, which transforms elements but does not select.
3fill in blank
hard

Fix the error in the code to map numbers to their squares.

Kotlin
val numbers = sequenceOf(1, 2, 3)
val squares = numbers.[1] { it * it }
Drag options to blanks, or click blank then click option'
Afilter
Bfold
Creduce
Dmap
Attempts:
3 left
💡 Hint
Common Mistakes
Using filter which selects elements, not transforms them.
4fill in blank
hard

Fill both blanks to create a sequence of squares of even numbers.

Kotlin
val numbers = listOf(1, 2, 3, 4, 5)
val result = numbers.asSequence().[1] { it % 2 == 0 }.[2] { it * it }.toList()
Drag options to blanks, or click blank then click option'
Afilter
Bmap
Creduce
Dfold
Attempts:
3 left
💡 Hint
Common Mistakes
Swapping filter and map order, which changes the result.
5fill in blank
hard

Fill all three blanks to create a sequence of uppercase strings longer than 3 characters.

Kotlin
val words = listOf("cat", "dog", "elephant", "fox")
val result = words.asSequence().[1] { it.length > 3 }.[2] { it.[3]() }.toList()
Drag options to blanks, or click blank then click option'
Afilter
Bmap
Cuppercase
Dlowercase
Attempts:
3 left
💡 Hint
Common Mistakes
Using lowercase instead of uppercase.
Swapping filter and map order.