Created
March 6, 2023 15:57
-
-
Save jeroenr/b13a75e0cd7c890f8a147ab7d0447610 to your computer and use it in GitHub Desktop.
Revisions
-
jeroenr created this gist
Mar 6, 2023 .There are no files selected for viewing
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 charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,8 @@ 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 } } }