0
0
Kotlinprogramming~5 mins

Partition for splitting in Kotlin - Cheat Sheet & Quick Revision

Choose your learning style9 modes available
Recall & Review
beginner
What does the partition function do in Kotlin?
It splits a collection into two lists based on a condition: one list with elements that satisfy the condition, and another with elements that don't.
Click to reveal answer
beginner
How do you use partition to separate even and odd numbers from a list?
Use val (evens, odds) = list.partition { it % 2 == 0 }. This creates two lists: evens with even numbers and odds with odd numbers.
Click to reveal answer
intermediate
What is the return type of the partition function?
It returns a Pair<List<T>, List<T>>, where the first list contains elements matching the predicate, and the second list contains the rest.
Click to reveal answer
beginner
Can partition be used on any Kotlin collection?
Yes, partition works on any Iterable, like lists and sets.
Click to reveal answer
intermediate
What happens if no elements satisfy the condition in partition?
The first list in the returned pair will be empty, and the second list will contain all elements.
Click to reveal answer
What does Kotlin's partition function return?
AA single list with filtered elements
BA map of elements grouped by condition
CA pair of lists splitting elements by a condition
DA boolean indicating if any element matches
How do you capture the two lists returned by partition?
AUsing destructuring declaration like <code>val (yes, no) = ...</code>
BUsing a single list variable
CUsing a map variable
DUsing a boolean variable
If you want to split a list of numbers into positives and non-positives, which predicate fits partition?
A<code>{ it >= 0 }</code>
B<code>{ it < 0 }</code>
C<code>{ it == 0 }</code>
D<code>{ it > 0 }</code>
What type of collections can use partition in Kotlin?
AAny <code>Iterable</code> like lists and sets
BOnly arrays
COnly maps
DOnly sequences
What will be the size of the first list if no elements satisfy the partition condition?
AUndefined
BZero (empty list)
COne
DSame as original list
Explain how Kotlin's partition function works and give a simple example.
Think about separating items based on a yes/no question.
You got /4 concepts.
    Describe the return type of partition and how to access the two lists it produces.
    Remember how Kotlin lets you unpack pairs easily.
    You got /4 concepts.