Your task is to implement the partitions function, which returns a set of all possible partitions of a given set. A partition of a set is a grouping of its elements into non-empty subsets, in such a way that every element is included in exactly one subset. Here are a few examples of how it should work:
fun <T> Collection<T>.partitions(): Set<Set<Set<T>>> = TODO()
It is easiest to implement this function using recursion.
This problem can either be solved in the below playground or you can clone kotlin-exercises project and solve it locally. In the project, you can find code template for this exercise in functional/collections/Partitions.kt. You can find there starting code and unit tests.
Once you are done with the exercise, you can check your solution here.
Playground
import org.junit.Test
import kotlin.test.assertEquals
fun <T> Collection<T>.partitions(): Set<Set<Set<T>>> = TODO()
class PartitionsTest {
@Test
fun `partitions of two elements`() {
val result = setOf(1, 2).partitions()
val expected = setOf(
setOf(setOf(1), setOf(2)),
setOf(setOf(1, 2))
)
assertEquals(expected, result)
}
@Test
fun `partitions of three elements`() {
val result = listOf("A", "B", "C").partitions()
val expected = setOf(
setOf(setOf("A"), setOf("B"), setOf("C")),
setOf(setOf("A"), setOf("B", "C")),
setOf(setOf("A", "B"), setOf("C")),
setOf(setOf("A", "C"), setOf("B")),
setOf(setOf("A", "B", "C"))
)
assertEquals(expected, result)
}
@Test
fun `partitions of four elements`() {
val result = listOf("A", "B", "C", "D").partitions()
val expected = setOf(
setOf(setOf("D"), setOf("C"), setOf("B"), setOf("A")),
setOf(setOf("D", "C"), setOf("B"), setOf("A")),
setOf(setOf("C"), setOf("D", "B"), setOf("A")),
setOf(setOf("D"), setOf("C", "B"), setOf("A")),
setOf(setOf("D", "C", "B"), setOf("A")),
setOf(setOf("C"), setOf("B"), setOf("D", "A")),
setOf(setOf("D"), setOf("B"), setOf("C", "A")),
setOf(setOf("D"), setOf("C"), setOf("B", "A")),
setOf(setOf("B"), setOf("D", "C", "A")),
setOf(setOf("D", "C"), setOf("B", "A")),
setOf(setOf("D", "B"), setOf("C", "A")),
setOf(setOf("C"), setOf("D", "B", "A")),
setOf(setOf("C", "B"), setOf("D", "A")),
setOf(setOf("D"), setOf("C", "B", "A")),
setOf(setOf("D", "C", "B", "A"))
)
assertEquals(expected, result)
}
}
Marcin Moskala is a highly experienced developer and Kotlin instructor as the founder of Kt. Academy, an official JetBrains partner specializing in Kotlin training, Google Developers Expert, known for his significant contributions to the Kotlin community. Moskala is the author of several widely recognized books, including "Effective Kotlin," "Kotlin Coroutines," "Functional Kotlin," "Advanced Kotlin," "Kotlin Essentials," and "Android Development with Kotlin."
Beyond his literary achievements, Moskala is the author of the largest Medium publication dedicated to Kotlin. As a respected speaker, he has been invited to share his insights at numerous programming conferences, including events such as Droidcon and the prestigious Kotlin Conf, the premier conference dedicated to the Kotlin programming language.