Closed natecook1000 closed 3 years ago
I think we actually want uniquePermutations(ofCount:)
.
I just wanted this for #107 where I wrote:
var tests: [[Int?]] = [
[],
[0],
[nil],
[0, nil],
[nil, 0],
[0, nil, 1, nil, 2, nil],
[0, 1, 2, nil, nil, nil],
[nil, nil, nil, 0, 1, 2],
]
I would have preferred to write:
let tests = [nil, nil, nil, 0, 1, 2].uniquePermutations(ofCount: 0)
... which also would have given me more exhaustive test coverage.
Totally agreed, though that points to the need for a by areInIncreasingOrder
version as well, since optionals are no longer Comparable
. Luckily, the closure's easy to write! 😜
let tests = [nil, nil, nil, 0, 1, 2]
.uniquePermutations(ofCount: 0...) { lhs, rhs in
rhs.map { rhs in lhs.map { lhs in lhs < rhs } ?? true } ?? false
}
@swift-ci Please test
@swift-ci Please test
@swift-ci Please test
@swift-ci Please test
Description
Adds methods for generating the unique permutations of a sequence, as distinct from the position-based permutations generated by
permutations()
.Detailed Design
Includes new
uniquePermutations(ofCount:)
methods (for both single counts and range expressions) and the wrappingUniquePermutations
type. The method requires that a collection's elements be hashable so that uniqueness can be determined in a pre-processing pass:Documentation Plan
I've added API documentation on the new methods and type, and have updated
Permutations.md
to include information about unique permutations.Test Plan
I've created tests in the
UniquePermutationsTests.swift
file.Source Impact
None, additive only.
Checklist