Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/arrays/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios 在Swift中将对象数组排序为每个可能的序列_Ios_Arrays_Swift_Sorting - Fatal编程技术网

Ios 在Swift中将对象数组排序为每个可能的序列

Ios 在Swift中将对象数组排序为每个可能的序列,ios,arrays,swift,sorting,Ios,Arrays,Swift,Sorting,想知道在Swift中是否有一种干净的方法可以做到这一点。可能使用一个或几个全局函数,如Map/Reduce等 数组包含数量为n的唯一自定义对象 例如,有3个项目。但可能会有更多或更少[1,2,3] 将返回一个数组数组 [ [1, 2, 3] [1, 3, 2] [2, 1, 3] [2, 3, 1] [3, 1, 2] [3, 2, 1] ] 完成任务。只需要进入Swift状态。 他创建了结构来处理排列: var greetingPermutations = PermutationSequenc

想知道在Swift中是否有一种干净的方法可以做到这一点。可能使用一个或几个全局函数,如Map/Reduce等

数组包含数量为n的唯一自定义对象

例如,有3个项目。但可能会有更多或更少[1,2,3]

将返回一个数组数组

[ [1, 2, 3] [1, 3, 2] [2, 1, 3] [2, 3, 1] [3, 1, 2] [3, 2, 1] ]

完成任务。只需要进入Swift状态。

他创建了结构来处理排列:

var greetingPermutations = PermutationSequenceGenerator(elements: ["hi", "hey", "hello"])
while let greetingSequence = greetingPermutations.next(){
    for greeting in greetingSequence {
    print("\(greeting) ")
   }
   println()
}
或:

编辑

下面是在objc.io上找到的一种更简单的方法

添加扩展名

extension Array {
    var decompose : (head: T, tail: [T])? {
        return (count > 0) ? (self[0], Array(self[1..<count])) : nil 
    }
}

这段代码使用分解函数扩展了数组,还添加了>>==操作符(展平)更多关于展平的内容:

可能太过c-ish,但这里是已经发布的示例的替代方案

var a = [1, 2, 3, 4, 5]
var b = [[Int]]()

func perms<T>(n: Int, inout a: [T], inout b: [[T]]) {
    if n == 0 {
        b.append(a)
    } else {
        for i in 0..<n {
            perms(n - 1, &a, &b)
            var j = 0
            if n % 2 == 0 {
                j = i
            }
            swap(&a[j], &a[n - 1])
        }
    }
}


perms(a.count, &a, &b)
println(b)
var a=[1,2,3,4,5]
变量b=[[Int]]()
函数perms(n:Int,inout a:[T],inout b:[T]]{
如果n==0{
b、 附加(a)
}否则{

对于0中的i..Swift 5

更新版本的@DogCoffee for swift 5.x,均在阵列扩展中:

extension Array {
    private var decompose : (head: Element, tail: [Element])? {
        return (count > 0) ? (self[0], Array(self[1..<count])) : nil
    }
    
    private func between<T>(x: T, ys: [T]) -> [[T]] {
        if let (head, tail) = ys.decompose {
            return [[x] + ys] + between(x: x, ys: tail).map { [head] + $0 }
        } else {
            return [[x]]
        }
    }
    
    private func permutations<T>(xs: [T]) -> [[T]] {
        if let (head, tail) = xs.decompose {
            return permutations(xs: tail) >>= { permTail in
                self.between(x: head, ys: permTail)
            }
        } else {
            return [[]]
        }
    }
    
    func allPermutations() -> [[Element]] {
        return permutations(xs: self)
    } 
}

infix operator >>=
func >>=<A, B>(xs: [A], f: (A) -> [B]) -> [B] {
    return xs.map(f).reduce([], +)
}
扩展数组{
私有var分解:(head:Element,tail:[Element]){
返回(计数>0)?(自[0],数组(自[1..[[T]]{
如果让(头、尾)=ys分解{
返回[[x]+ys]+between(x:x,ys:tail).map{[head]+$0}
}否则{
返回[[x]]
}
}
私有函数置换(xs:[T])->[[T]]{
如果let(head,tail)=xs.decompose{
返回置换(xs:tail)>>={permTail in
self.between(x:head,ys:permTail)
}
}否则{
返回[]]
}
}
func allPermutations()->[[Element]]{
返回置换(xs:self)
} 
}
中缀运算符>>=
func>>=(xs[A],f:(A)->[B])->[B]{
返回xs.map(f).reduce([],+)
}

what is between in:between(head,permTail)对不起,忘了粘贴between函数
func between<T>(x: T, ys: [T]) -> [[T]] {
    if let (head, tail) = ys.decompose {
        return [[x] + ys] + between(x, ys: tail).map { [head] + $0 }
    } else {
        return [[x]]
    }
}


func permutations<T>(xs: [T]) -> [[T]] {
    if let (head, tail) = xs.decompose {
        return permutations(tail) >>= { permTail in
            self.between(head, ys: permTail)
        }
    } else {
        return [[]]
    }
}
    let example = permutations([1,2,3,5,6,7,8])
    println(example)
var a = [1, 2, 3, 4, 5]
var b = [[Int]]()

func perms<T>(n: Int, inout a: [T], inout b: [[T]]) {
    if n == 0 {
        b.append(a)
    } else {
        for i in 0..<n {
            perms(n - 1, &a, &b)
            var j = 0
            if n % 2 == 0 {
                j = i
            }
            swap(&a[j], &a[n - 1])
        }
    }
}


perms(a.count, &a, &b)
println(b)
extension Array {
    private var decompose : (head: Element, tail: [Element])? {
        return (count > 0) ? (self[0], Array(self[1..<count])) : nil
    }
    
    private func between<T>(x: T, ys: [T]) -> [[T]] {
        if let (head, tail) = ys.decompose {
            return [[x] + ys] + between(x: x, ys: tail).map { [head] + $0 }
        } else {
            return [[x]]
        }
    }
    
    private func permutations<T>(xs: [T]) -> [[T]] {
        if let (head, tail) = xs.decompose {
            return permutations(xs: tail) >>= { permTail in
                self.between(x: head, ys: permTail)
            }
        } else {
            return [[]]
        }
    }
    
    func allPermutations() -> [[Element]] {
        return permutations(xs: self)
    } 
}

infix operator >>=
func >>=<A, B>(xs: [A], f: (A) -> [B]) -> [B] {
    return xs.map(f).reduce([], +)
}