Last active
September 4, 2018 09:20
-
-
Save martinr448/3725daf836d41293b0b6fb5c1a5b4718 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
import Foundation | |
func permute<E>(i: Int, array: [E]) -> [E] { | |
var i = i | |
var result = array | |
for j in 0..<array.count { | |
let k = i % (array.count - j) | |
i /= (array.count - j) | |
result.swapAt(j, j + k) | |
} | |
return result | |
} | |
for i in 0 ..< 1*2*3 { | |
print(permute(i: i, array: ["a", "b", "c"]).joined()) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment