Complete the code to filter even numbers from the list.
val numbers = listOf(1, 2, 3, 4, 5) val evens = numbers.filter { it [1] 2 == 0 } println(evens)
The filter function keeps elements where the condition is true. Using % 2 == 0 selects even numbers.
Complete the code to filter out odd numbers using filterNot.
val numbers = listOf(1, 2, 3, 4, 5) val evens = numbers.filterNot { it [1] 2 != 0 } println(evens)
The filterNot function excludes elements where the condition is true. Using % 2 != 0 excludes odd numbers, keeping evens.
Fix the error in the code to filter words longer than 3 characters.
val words = listOf("cat", "dog", "elephant", "fox") val longWords = words.filter { it.length [1] 3 } println(longWords)
To get words longer than 3 characters, use > operator comparing length to 3.
Fill both blanks to create a map of word lengths for words shorter than 5 characters.
val words = listOf("apple", "bat", "cat", "dog", "elephant") val shortWordLengths = words.filter { it.length [1] 5 }.associateWith { it.[2] } println(shortWordLengths)
Filter words with length less than 5 using <. Then map each word to its length using length.
Fill all three blanks to create a map of uppercase words to their lengths for words not starting with 'a'.
val words = listOf("apple", "bat", "cat", "dog", "ant") val result = words.filterNot { it.startsWith([1]) }.associateWith { it.[2] }.mapKeys { it.key.[3]() } println(result)
Filter out words starting with "a" using startsWith("a"). Map values to their length. Change keys to uppercase using toUpperCase().