📖 Scenario: Imagine you have a list of numbers representing daily temperatures. You want to separate these temperatures into two groups: those that are warm (above a certain threshold) and those that are cool (at or below that threshold).
🎯 Goal: You will create a Kotlin program that uses the partition function to split a list of temperatures into warm and cool days.
📋 What You'll Learn
Create a list of integers called temperatures with the exact values: 15, 22, 8, 30, 18, 5
Create an integer variable called threshold and set it to 20
Use the partition function on temperatures with a condition that checks if a temperature is greater than threshold
Print the two resulting lists with the exact variable names warmDays and coolDays
💡 Why This Matters
🌍 Real World
Partitioning data helps in sorting and organizing information, like separating warm and cool days for weather reports.
💼 Career
Understanding how to split data based on conditions is useful in data analysis, software development, and creating user-friendly applications.
Progress0 / 4 steps
1
Create the list of temperatures
Create a list of integers called temperatures with these exact values: 15, 22, 8, 30, 18, 5
Kotlin
Hint
Use listOf to create the list with the exact numbers given.
2
Set the threshold value
Create an integer variable called threshold and set it to 20
Kotlin
Hint
Use val threshold = 20 to create the variable.
3
Partition the temperatures
Use the partition function on temperatures with a condition that checks if a temperature is greater than threshold. Store the first list in warmDays and the second list in coolDays
Kotlin
Hint
Use val (warmDays, coolDays) = temperatures.partition { it > threshold } to split the list.
4
Print the warm and cool days
Print the lists warmDays and coolDays exactly using two separate println statements
Kotlin
Hint
Use println(warmDays) and println(coolDays) to show the results.
Practice
(1/5)
1. What does the partition function do in Kotlin when applied to a list?
easy
A. Converts the list into a map with indices as keys.
B. Sorts the list in ascending order.
C. Removes duplicate elements from the list.
D. Splits the list into two lists based on a condition, returning a Pair of lists.
Solution
Step 1: Understand the purpose of partition
The partition function divides a list into two parts based on a condition.
Step 2: Recognize the return type
It returns a Pair where the first list contains elements matching the condition, and the second list contains the rest.
Final Answer:
Splits the list into two lists based on a condition, returning a Pair of lists. -> Option D
Quick Check:
partition = split by condition [OK]
Hint: Remember: partition always returns two lists in a Pair [OK]
Common Mistakes:
Thinking partition sorts the list
Confusing partition with filtering
Assuming partition removes duplicates
2. Which of the following is the correct syntax to use partition on a list numbers to split even and odd numbers?
easy
A. val (evens, odds) = numbers.partition { it % 2 == 0 }
B. val (evens, odds) = numbers.partition(it % 2 == 0)
C. val evens = numbers.partition { it % 2 == 0 }
D. val evens, odds = numbers.partition { it % 2 == 0 }
Solution
Step 1: Check the lambda syntax inside partition
The condition must be inside curly braces with it representing each element.
Step 2: Verify destructuring declaration
Partition returns a Pair, so use val (first, second) = ... to get both lists.
Final Answer:
val (evens, odds) = numbers.partition { it % 2 == 0 } -> Option A
Quick Check:
Correct lambda and destructuring = val (evens, odds) = numbers.partition { it % 2 == 0 } [OK]
Hint: Use val (a, b) = list.partition { condition } [OK]
Common Mistakes:
Missing curly braces for lambda
Not using destructuring for Pair
Using parentheses instead of braces for lambda
3. What is the output of this Kotlin code?
val words = listOf("apple", "banana", "avocado", "berry")
val (aWords, otherWords) = words.partition { it.startsWith('a') }
println(aWords)
println(otherWords)
medium
A. ["apple", "avocado"]
["banana", "berry"]
B. ["banana", "berry"]
["apple", "avocado"]
C. ["apple"]
["banana", "avocado", "berry"]
D. ["avocado"]
["apple", "banana", "berry"]
Solution
Step 1: Apply the condition to each word
Check which words start with 'a': "apple" and "avocado" match.
Step 2: Partition splits into two lists
First list has words starting with 'a', second list has the rest: "banana", "berry".
Final Answer:
["apple", "avocado"]
["banana", "berry"] -> Option A
Quick Check:
partition by startsWith('a') = ["apple", "avocado"]
["banana", "berry"] [OK]
Hint: Check condition on each element carefully [OK]
Common Mistakes:
Mixing order of lists in output
Forgetting that partition returns Pair of lists
Confusing startsWith with contains
4. Identify the error in this Kotlin code using partition:
val nums = listOf(1, 2, 3, 4)
val evens, odds = nums.partition { it % 2 == 0 }
println(evens)
println(odds)
medium
A. partition cannot be used on lists of integers.
B. The lambda condition is incorrect; should be it % 2 != 0.
C. Missing parentheses around evens and odds for destructuring declaration.
D. println statements are missing string interpolation.
Solution
Step 1: Check destructuring declaration syntax
Destructuring a Pair requires parentheses: val (evens, odds) = ....
Step 2: Confirm lambda and usage
The lambda is correct for evens; partition works on lists of integers; println is fine.
Final Answer:
Missing parentheses around evens and odds for destructuring declaration. -> Option C
Quick Check:
Destructuring needs parentheses [OK]
Hint: Use val (a, b) = ... for destructuring Pair [OK]
Common Mistakes:
Omitting parentheses in destructuring
Changing correct lambda unnecessarily
Thinking partition only works on strings
5. Given a list of mixed integers including zero and negatives, how can you use partition to separate positive numbers from zero and negatives?
Choose the correct code snippet.
hard
A. val (positives, nonPositives) = numbers.partition { it < 0 }
B. val (positives, nonPositives) = numbers.partition { it > 0 }
C. val (positives, nonPositives) = numbers.partition { it >= 0 }
D. val (positives, nonPositives) = numbers.partition { it == 0 }
Solution
Step 1: Define the condition for positives
Positive numbers are greater than zero, so condition is it > 0.
Step 2: Use partition with this condition
Partition splits list into positives and the rest (zero and negatives).
Final Answer:
val (positives, nonPositives) = numbers.partition { it > 0 } -> Option B
Quick Check:
Partition by it > 0 separates positives [OK]
Hint: Use it > 0 to get positives, rest are zero/negatives [OK]
Common Mistakes:
Using it >= 0 includes zero as positive
Using it < 0 reverses groups
Using it == 0 only selects zeros
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.