September 2017 Update
This commit is contained in:
parent
bba7bfd280
commit
ba8067c3b7
14570 changed files with 153136 additions and 63871 deletions
38
Task/Power-set/Kotlin/power-set.kotlin
Normal file
38
Task/Power-set/Kotlin/power-set.kotlin
Normal file
|
|
@ -0,0 +1,38 @@
|
|||
// version 1.1.3
|
||||
|
||||
class PowerSet<T>(val items: List<T>) {
|
||||
private lateinit var combination: IntArray
|
||||
|
||||
init {
|
||||
println("Power set of $items comprises:")
|
||||
for (m in 0..items.size) {
|
||||
combination = IntArray(m)
|
||||
generate(0, m)
|
||||
}
|
||||
}
|
||||
|
||||
private fun generate(k: Int, m: Int) {
|
||||
if (k >= m) {
|
||||
println(combination.map { items[it] })
|
||||
}
|
||||
else {
|
||||
for (j in 0 until items.size)
|
||||
if (k == 0 || j > combination[k - 1]) {
|
||||
combination[k] = j
|
||||
generate(k + 1, m)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fun main(args: Array<String>) {
|
||||
val itemsList = listOf(
|
||||
listOf(1, 2, 3, 4),
|
||||
emptyList<Int>(),
|
||||
listOf(emptyList<Int>())
|
||||
)
|
||||
for (items in itemsList) {
|
||||
PowerSet(items)
|
||||
println()
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue