Created
March 6, 2023 15:57
-
-
Save jeroenr/b13a75e0cd7c890f8a147ab7d0447610 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
fun <T> List<T>.permutations(): List<List<T>> = | |
when { | |
size < 2 -> listOf(this) | |
size == 2 -> listOf(listOf(first(), last()), listOf(last(), first())) | |
else -> flatMap { element -> | |
(this - element).permutations().map { listOf(element) + it } | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment