0
0
Kotlinprogramming~15 mins

Partition for splitting in Kotlin - Mini Project: Build & Apply

Choose your learning style9 modes available
Partition for splitting
📖 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
Need a 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
Need a 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
Need a 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
Need a hint?

Use println(warmDays) and println(coolDays) to show the results.