0
0
Kotlinprogramming~10 mins

When to use sequences 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'
AtoList
BasSequence
Cmap
Dfilter
Attempts:
3 left
💡 Hint
Common Mistakes
Using toList() instead of asSequence().
2fill in blank
medium

Complete the code to lazily filter even numbers using sequences.

Kotlin
val seq = sequenceOf(1, 2, 3, 4, 5)
val evens = seq.[1] { it % 2 == 0 }
Drag options to blanks, or click blank then click option'
Afilter
Bmap
Creduce
Dfold
Attempts:
3 left
💡 Hint
Common Mistakes
Using map which transforms elements instead of filtering.
3fill in blank
hard

Fix the error in the code to convert a list to a sequence and map values.

Kotlin
val list = listOf(1, 2, 3)
val result = list.[1]().map { it * 2 }.toList()
Drag options to blanks, or click blank then click option'
AasSequence
BtoList
CsequenceOf
Dfilter
Attempts:
3 left
💡 Hint
Common Mistakes
Using toList() which returns the same list, not a sequence.
4fill in blank
hard

Fill both blanks to create a sequence from a list and lazily map values.

Kotlin
val list = listOf(1, 2, 3, 4)
val seq = list.[1]().[2] { it + 1 }
Drag options to blanks, or click blank then click option'
AasSequence
Bfilter
Cmap
DtoList
Attempts:
3 left
💡 Hint
Common Mistakes
Using toList() before map which is eager, not lazy.
5fill in blank
hard

Fill all three blanks to create a sequence, filter even numbers, and convert to list.

Kotlin
val numbers = listOf(1, 2, 3, 4, 5)
val evens = numbers.[1]().[2] { it % 2 == 0 }.[3]()
Drag options to blanks, or click blank then click option'
AasSequence
Bfilter
CtoList
Dmap
Attempts:
3 left
💡 Hint
Common Mistakes
Using map instead of filter to select even numbers.