[DOC] Tweaks for Array#permutation (#11802)

This commit is contained in:
Burdette Lamar 2024-10-07 14:21:47 -05:00 committed by GitHub
parent 4cbd2ab9d4
commit 05d3b727d6
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
Notes: git 2024-10-07 19:22:05 +00:00
Merged-By: peterzhu2118 <peter@peterzhu.ca>

82
array.c
View File

@ -7000,82 +7000,44 @@ rb_ary_permutation_size(VALUE ary, VALUE args, VALUE eobj)
/* /*
* call-seq: * call-seq:
* array.permutation {|element| ... } -> self * permutation(n = self.size) {|permutation| ... } -> self
* array.permutation(n) {|element| ... } -> self * permutation(n = self.size) -> new_enumerator
* array.permutation -> new_enumerator
* array.permutation(n) -> new_enumerator
* *
* When invoked with a block, yield all permutations of elements of +self+; returns +self+. * Iterates over permutations of the elements of +self+;
* The order of permutations is indeterminate. * the order of permutations is indeterminate.
* *
* When a block and an in-range positive Integer argument +n+ (<tt>0 < n <= self.size</tt>) * With a block and an in-range positive integer argument +n+ (<tt>0 < n <= self.size</tt>) given,
* are given, calls the block with all +n+-tuple permutations of +self+. * calls the block with each +n+-tuple permutations of +self+;
* * returns +self+:
* Example:
* *
* a = [0, 1, 2] * a = [0, 1, 2]
* a.permutation(2) {|permutation| p permutation } * perms = []
* a.permutation(1) {|perm| perms.push(perm) }
* perms # => [[0], [1], [2]]
* *
* Output: * perms = []
* a.permutation(2) {|perm| perms.push(perm) }
* perms # => [[0, 1], [0, 2], [1, 0], [1, 2], [2, 0], [2, 1]]
* *
* [0, 1] * perms = []
* [0, 2] * a.permutation(3) {|perm| perms.push(perm) }
* [1, 0] * perms # => [[0, 1, 2], [0, 2, 1], [1, 0, 2], [1, 2, 0], [2, 0, 1], [2, 1, 0]]
* [1, 2]
* [2, 0]
* [2, 1]
* *
* Another example: * When +n+ is zero, calls the block once with a new empty array:
* *
* a = [0, 1, 2] * perms = []
* a.permutation(3) {|permutation| p permutation } * a.permutation(0) {|perm| perms.push(perm) }
* * perms # => [[]]
* Output:
*
* [0, 1, 2]
* [0, 2, 1]
* [1, 0, 2]
* [1, 2, 0]
* [2, 0, 1]
* [2, 1, 0]
*
* When +n+ is zero, calls the block once with a new empty +Array+:
*
* a = [0, 1, 2]
* a.permutation(0) {|permutation| p permutation }
*
* Output:
*
* []
* *
* When +n+ is out of range (negative or larger than <tt>self.size</tt>), * When +n+ is out of range (negative or larger than <tt>self.size</tt>),
* does not call the block: * does not call the block:
* *
* a = [0, 1, 2]
* a.permutation(-1) {|permutation| fail 'Cannot happen' } * a.permutation(-1) {|permutation| fail 'Cannot happen' }
* a.permutation(4) {|permutation| fail 'Cannot happen' } * a.permutation(4) {|permutation| fail 'Cannot happen' }
* *
* When a block given but no argument, * With no block given, returns a new Enumerator.
* behaves the same as <tt>a.permutation(a.size)</tt>:
*
* a = [0, 1, 2]
* a.permutation {|permutation| p permutation }
*
* Output:
*
* [0, 1, 2]
* [0, 2, 1]
* [1, 0, 2]
* [1, 2, 0]
* [2, 0, 1]
* [2, 1, 0]
*
* Returns a new Enumerator if no block given:
*
* a = [0, 1, 2]
* a.permutation # => #<Enumerator: [0, 1, 2]:permutation>
* a.permutation(2) # => #<Enumerator: [0, 1, 2]:permutation(2)>
* *
* Related: {Methods for Iterating}[rdoc-ref:Array@Methods+for+Iterating].
*/ */
static VALUE static VALUE