You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Add uniquePermutations as wrapper for nextPermutation (#91)
* Add uniquePermutations as wrapper for nextPermutation
* Implement uniquePermutations(ofCount:) variants
* Simplify UniquePermutations.Iterator.next()
* Add tests for uniquePermutations
* Move to a predicate-based uniquePermutations
* Add method documentation
* Convert to Hashable-based uniquing instead of Comparable
* Streamline tests, cover uniqueness
* Use optional count to collapse no-count method
* Update API documentation
* Remove leftover version of nextPermutation()
* Remove Hashable requirement from the UniquePermutations type
* Update documentation
* Apply Tim's great revisions
Co-authored-by: Tim Vermeulen <[email protected]>
* Add lazy conformance to UniquePermutations
Co-authored-by: Tim Vermeulen <[email protected]>
A type that computes permutations of a collection’s elements, or of a subset of
6
+
Methods that compute permutations of a collection’s elements, or of a subset of
7
7
those elements.
8
8
9
9
The `permutations(ofCount:)` method, when called without the `ofCount`
@@ -60,7 +60,18 @@ for perm in numbers2.permutations() {
60
60
// [10, 10, 20]
61
61
```
62
62
63
-
Given a range, the `permutations(ofCount:)` method returns a sequence of all the different permutations of the given sizes of a collection’s elements in increasing order of size.
63
+
To generate only unique permutations, use the `uniquePermutations(ofCount:)` method:
64
+
65
+
```swift
66
+
for perm in numbers2.uniquePermutations() {
67
+
print(perm)
68
+
}
69
+
// [20, 10, 10]
70
+
// [10, 20, 10]
71
+
// [10, 10, 20]
72
+
```
73
+
74
+
Given a range, the methods return a sequence of all the different permutations of the given sizes of a collection’s elements in increasing order of size.
64
75
65
76
```swift
66
77
let numbers = [10, 20, 30]
@@ -87,28 +98,40 @@ for perm in numbers.permutations(ofCount: 0...) {
87
98
88
99
## Detailed Design
89
100
90
-
The `permutations(ofCount:)`method is declared as a `Collection`extension,
91
-
and returns a `Permutations`type:
101
+
The `permutations(ofCount:)`and `uniquePermutations(ofCount:)` methods are declared as `Collection`extensions,
102
+
and return `Permutations`and `UniquePermutations` instances, respectively:
Copy file name to clipboardExpand all lines: README.md
+1Lines changed: 1 addition & 0 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -10,6 +10,7 @@ Read more about the package, and the intent behind it, in the [announcement on s
10
10
11
11
-[`combinations(ofCount:)`](https://github.com/apple/swift-algorithms/blob/main/Guides/Combinations.md): Combinations of particular sizes of the elements in a collection.
12
12
-[`permutations(ofCount:)`](https://github.com/apple/swift-algorithms/blob/main/Guides/Permutations.md): Permutations of a particular size of the elements in a collection, or of the full collection.
13
+
-[`uniquePermutations(ofCount:)`](https://github.com/apple/swift-algorithms/blob/main/Guides/Permutations.md): Permutations of a collection's elements, skipping any duplicate permutations.
0 commit comments