在Swift中获取数组中所有可能的项目组合,而无需重复组

时间:2020-12-14 07:35:09

I'm trying to create an extension on Array where I can get all possible combinations of an array without generating duplicate groups, including a no item combination.

我正在尝试在数组上创建一个扩展,在这里我可以得到一个数组的所有可能的组合,而不生成重复的组,包括一个无项组合。

For example, for this array:

例如,对于这个数组:

[1, 2, 3, 4]

The following possible combinations should be generated:

应产生下列可能的组合:

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

Please note that none of the groups repeat themselves, i.e: if there is a group [1, 2] there is no other group: [2, 1].

请注意,没有任何组重复它们自己,i。e:如果有一个群体[1,2],就没有其他群体[2,1]。

This is the closest I've been able to get to a result:

这是我所能得到的最接近结果的结果:

public extension Array {

func allPossibleCombinations() -> [[Element]] {
    var output: [[Element]] = [[]]
    for groupSize in 1...self.count {
        for (index1, item1) in self.enumerated() {
            var group = [item1]
            for (index2, item2) in self.enumerated() {
                if group.count < groupSize {
                    if index2 > index1 {
                        group.append(item2)
                        if group.count == groupSize {
                            output.append(group)
                            group = [item1]
                            continue
                        }
                    }
                } else {
                    break
                }
            }
            if group.count == groupSize {
                output.append(group)
            }
        }
    }
    return output
}

}

But it is missing possible combination of items in the group size 3 (I only get back [1, 2, 3] and [2, 3, 4].

但是它缺少组码3中项目的可能组合(我只返回[1,2,3]和[2,3,4]。

Much appreciated!

感谢!

2 个解决方案

#1


2  

You can use flatMap also to combine them in one line.

也可以使用flatMap将它们合并到一行中。

    extension Array {
        var combinationsWithoutRepetition: [[Element]] {
            guard !isEmpty else { return [[]] }
            return Array(self[1...]).combinationsWithoutRepetition.flatMap { [$0, [self[0]] + $0] }
        }
    }

 print([1,2,3,4].combinationsWithoutRepetition)

#2


3  

extension Array {
    var combinations: [[Element]] {
        if count == 0 {
            return [self]
        }
        else {
            let tail = Array(self[1..<endIndex])
            let head = self[0]

            let first = tail.combinations
            let rest = first.map { $0 + [head] }

            return first + rest
        }
    }
}

print([1, 2, 3, 4].combinations)

#1


2  

You can use flatMap also to combine them in one line.

也可以使用flatMap将它们合并到一行中。

    extension Array {
        var combinationsWithoutRepetition: [[Element]] {
            guard !isEmpty else { return [[]] }
            return Array(self[1...]).combinationsWithoutRepetition.flatMap { [$0, [self[0]] + $0] }
        }
    }

 print([1,2,3,4].combinationsWithoutRepetition)

#2


3  

extension Array {
    var combinations: [[Element]] {
        if count == 0 {
            return [self]
        }
        else {
            let tail = Array(self[1..<endIndex])
            let head = self[0]

            let first = tail.combinations
            let rest = first.map { $0 + [head] }

            return first + rest
        }
    }
}

print([1, 2, 3, 4].combinations)