Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete 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'
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.
✗ Incorrect
The groupBy function groups elements by the key returned by the lambda. Using it.first() groups words by their first letter.
2fill in blank
mediumComplete 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'
Attempts:
3 left
💡 Hint
Common Mistakes
Using '*' or '+' changes the number, not the remainder.
Using '/' gives the quotient, not remainder.
✗ Incorrect
The '%' operator gives the remainder. Grouping by 'it % 3' groups numbers by their remainder when divided by 3.
3fill in blank
hardFix 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'
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.
✗ Incorrect
In Kotlin, the property 'length' gives the length of a string. 'size' is for collections, 'length()' is incorrect (property, no ()), 'count' is a function.
4fill in blank
hardFill 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'
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'first()' groups by first letter, not last.
Using '==' 1 filters groups with exactly one word, not more.
✗ Incorrect
Using 'last()' groups words by their last letter. Filtering with '>' 1 keeps groups with more than one word.
5fill in blank
hardFill 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'
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.
✗ Incorrect
Use 'first()' to get the first letter, then 'lowercase()' to convert it to lowercase. Filter groups with size greater than 2.