Skip to content

Instantly share code, notes, and snippets.

@martinr448
Last active September 4, 2018 09:20
Show Gist options
  • Save martinr448/3725daf836d41293b0b6fb5c1a5b4718 to your computer and use it in GitHub Desktop.
Save martinr448/3725daf836d41293b0b6fb5c1a5b4718 to your computer and use it in GitHub Desktop.
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