0
0
Kotlinprogramming~10 mins

GroupBy for categorization 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 group the list of words by their first letter.

Kotlin
val words = listOf("apple", "banana", "apricot", "blueberry")
val grouped = words.groupBy { it.[1] }
println(grouped)
Drag options to blanks, or click blank then click option'
Afirst()
Blength
Clast()
Duppercase()
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'length' instead of 'first()' groups by word length, not first letter.
Using 'last()' groups by last letter, not first.
2fill in blank
medium

Complete the code to group numbers by their remainder when divided by 3.

Kotlin
val numbers = listOf(1, 2, 3, 4, 5, 6)
val grouped = numbers.groupBy { it [1] 3 }
println(grouped)
Drag options to blanks, or click blank then click option'
A%
B*
C/
D+
Attempts:
3 left
💡 Hint
Common Mistakes
Using '*' or '+' changes the number, not the remainder.
Using '/' gives the quotient, not remainder.
3fill in blank
hard

Fix the error in the code to group words by their length.

Kotlin
val words = listOf("cat", "dog", "bird")
val grouped = words.groupBy { it.[1] }
println(grouped)
Drag options to blanks, or click blank then click option'
Asize
Bcount
Clength
Dlength()
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'size' causes errors; that's for collections like lists, strings use 'length'.
Using 'length()' or 'count'; 'length' is a property (no ()), 'count()' is a function but not standard for length.
4fill in blank
hard

Fill both blanks to group words by their last character and filter groups with more than one word.

Kotlin
val words = listOf("apple", "angle", "banana", "bottle", "cat")
val grouped = words.groupBy { it.[1] }.filter { it.value.size [2] 1 }
println(grouped)
Drag options to blanks, or click blank then click option'
Alast()
B>
C==
Dfirst()
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'first()' groups by first letter, not last.
Using '==' 1 filters groups with exactly one word, not more.
5fill in blank
hard

Fill all three blanks to create a map of words grouped by the lowercase version of their first letter, including only groups with more than two words.

Kotlin
val words = listOf("apple", "apricot", "banana", "blueberry", "avocado", "blackberry")
val grouped = words.groupBy { it.[1].toString().[2]() }.filter { it.value.size [3] 2 }
println(grouped)
Drag options to blanks, or click blank then click option'
Afirst()
Bsize
C>
Dlowercase
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'size' as a function instead of property.
Using '>' incorrectly in filter condition.
Using 'toString()' unnecessarily or missing it.