Data commit
This commit is contained in:
parent
7387c8f97b
commit
cb5bb5e222
199093 changed files with 3378972 additions and 0 deletions
15
Task/Permutations/Swift/permutations.swift
Normal file
15
Task/Permutations/Swift/permutations.swift
Normal file
|
|
@ -0,0 +1,15 @@
|
|||
func perms<T>(var ar: [T]) -> [[T]] {
|
||||
return heaps(&ar, ar.count)
|
||||
}
|
||||
|
||||
func heaps<T>(inout ar: [T], n: Int) -> [[T]] {
|
||||
return n == 1 ? [ar] :
|
||||
Swift.reduce(0..<n, [[T]]()) {
|
||||
(var shuffles, i) in
|
||||
shuffles.extend(heaps(&ar, n - 1))
|
||||
swap(&ar[n % 2 == 0 ? i : 0], &ar[n - 1])
|
||||
return shuffles
|
||||
}
|
||||
}
|
||||
|
||||
perms([1, 2, 3]) // [[1, 2, 3], [2, 1, 3], [3, 1, 2], [1, 3, 2], [2, 3, 1], [3, 2, 1]]
|
||||
Loading…
Add table
Add a link
Reference in a new issue