September 2017 Update
This commit is contained in:
parent
bba7bfd280
commit
ba8067c3b7
14570 changed files with 153136 additions and 63871 deletions
22
Task/Permutations/Kotlin/permutations.kotlin
Normal file
22
Task/Permutations/Kotlin/permutations.kotlin
Normal file
|
|
@ -0,0 +1,22 @@
|
|||
// version 1.1.2
|
||||
|
||||
fun <T> permute(input: List<T>): List<List<T>> {
|
||||
if (input.size == 1) return listOf(input)
|
||||
val perms = mutableListOf<List<T>>()
|
||||
val toInsert = input[0]
|
||||
for (perm in permute(input.drop(1))) {
|
||||
for (i in 0..perm.size) {
|
||||
val newPerm = perm.toMutableList()
|
||||
newPerm.add(i, toInsert)
|
||||
perms.add(newPerm)
|
||||
}
|
||||
}
|
||||
return perms
|
||||
}
|
||||
|
||||
fun main(args: Array<String>) {
|
||||
val input = listOf('a', 'b', 'c', 'd')
|
||||
val perms = permute(input)
|
||||
println("There are ${perms.size} permutations of $input, namely:\n")
|
||||
for (perm in perms) println(perm)
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue