Skip to content

Commit 4ca1a21

Browse files
committed
Document range-based permutations(ofCount:)
1 parent 9c719b8 commit 4ca1a21

File tree

1 file changed

+49
-0
lines changed

1 file changed

+49
-0
lines changed

Sources/Algorithms/Permutations.swift

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -249,6 +249,55 @@ where Self: BidirectionalCollection, Element: Comparable
249249
//===----------------------------------------------------------------------===//
250250

251251
extension Collection {
252+
/// Returns a collection of the permutations of this collection with lengths
253+
/// in the specified range.
254+
///
255+
/// This example prints the different permutations of one to two elements from
256+
/// an array of three names:
257+
///
258+
/// let names = ["Alex", "Celeste", "Davide"]
259+
/// for perm in names.permutations(ofCount: 1...2) {
260+
/// print(perm.joined(separator: ", "))
261+
/// }
262+
/// // Alex
263+
/// // Celeste
264+
/// // Davide
265+
/// // Alex, Celeste
266+
/// // Alex, Davide
267+
/// // Celeste, Alex
268+
/// // Celeste, Davide
269+
/// // Davide, Alex
270+
/// // Davide, Celeste
271+
///
272+
/// This example prints _all_ the permutations (including an empty array) from
273+
/// the an array of numbers:
274+
///
275+
/// let numbers = [10, 20, 30]
276+
/// for perm in numbers.permutations(ofCount: 0...) {
277+
/// print(perm)
278+
/// }
279+
/// // []
280+
/// // [10]
281+
/// // [20]
282+
/// // [30]
283+
/// // [10, 20]
284+
/// // [10, 30]
285+
/// // [20, 10]
286+
/// // [20, 30]
287+
/// // [30, 10]
288+
/// // [30, 20]
289+
/// // [10, 20, 30]
290+
/// // [10, 30, 20]
291+
/// // [20, 10, 30]
292+
/// // [20, 30, 10]
293+
/// // [30, 10, 20]
294+
/// // [30, 20, 10]
295+
///
296+
/// - Parameter kRange: The number of elements to include in each permutation.
297+
///
298+
/// - Complexity: O(1) for random-access base collections. O(*n*) where *n*
299+
/// is the number of elements in the base collection, since `Permutations`
300+
/// accesses the `count` of the base collection.
252301
@inlinable
253302
public func permutations<R: RangeExpression>(
254303
ofCount kRange: R

0 commit comments

Comments
 (0)