fun <T> Collection<T>.powerset(): Set<Set<T>> {
TODO()
}
It is easiest to implement this function using recursion. Notice, that a powerset of a set with n elements is a powerset of a set with n-1 elements, plus all elements from the powerset of a set with n-1 elements with the n-th element added.
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/Powerset.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
// Powerset returns set of all subsets including full set and empty set
// https://en.wikipedia.org/wiki/Power_set
fun <T> Collection<T>.powerset(): Set<Set<T>> {
TODO()
}
fun main() {
println(setOf<Int>().powerset()) // [[]]
println(setOf("A").powerset()) // [[], [A]]
println(setOf('A', 'B').powerset()) // [[], [B], [A], [B, A]]
println(setOf(1, 2, 3).powerset())
// [[], [3], [2], [3, 2], [1], [3, 1], [2, 1], [3, 2, 1]]
println(setOf(1, 2, 3, 4).powerset())
// [[], [4], [3], [4, 3], [2], [4, 2], [3, 2], [4, 3, 2], [1], [4, 1], [3, 1], [4, 3, 1], [2, 1], [4, 2, 1], [3, 2, 1], [4, 3, 2, 1]]
}
class PowersetTest {
@Test
fun `Powerset of empty list is only empty list`() {
val emptyList = setOf<Int>()
assertEquals(setOf(emptyList), emptyList.powerset())
}
@Test
fun `Powerset of list with single element is empty list and single element`() {
assertEquals(setOf(setOf(1), setOf()), setOf(1).powerset())
}
@Test
fun `Powerset simple example test`() {
val set = setOf(
setOf(1, 2, 3),
setOf(1, 2),
setOf(1, 3),
setOf(2, 3),
setOf(1),
setOf(2),
setOf(3),
setOf()
)
assertEquals(set, setOf(1, 2, 3).powerset())
}
@Test
fun `Size of n element set powerset is 2^n`() {
for(n in 1..6) {
val set = (1..n).toSet()
val size = 2.pow(n)
assertEquals(size, set.powerset().size)
}
}
private fun Int.pow(power: Int): Int = Math.pow(this.toDouble(), power.toDouble()).toInt()
}
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.