| Class | Array |
| In: |
combinations.rb
permutations.rb |
| Parent: | Object |
Return a new array that is an arrangement of self based on the indices in the array that is passed as parameter.
# File permutations.rb, line 76 def arrange(a) return nil if a.length != self.length or a.any? {|x| x < 0 or x >= self.length} ret = [] 0.upto(self.length - 1) {|i| ret[i] = self[a[i]] } ret end
Generate the sequence of combinations taken k at a time for this Array. Return the number of such combinations. Return nil if the parameter is invalid. If no block is given, the sequence is not actually generated.
# File combinations.rb, line 72 def combinations(k) c = Combinations.new(self.length, k) if block_given? c.each { |a| yield self.select_indices(a) } else c.each end end
Yield a sequence of all permutations of self. Return the number of permutations.
# File permutations.rb, line 83 def permutations p = Permutations.new(self.length) if block_given? p.each {|a| yield self.arrange(a) } else p.each end end
Rotate self by moving the first element of the array to the end.
# File permutations.rb, line 71 def rotate! self << self.shift end
Return a new Array that is formed by selecting the indices given in the parameter. Return nil if any of the indices are out of range.
# File combinations.rb, line 82 def select_indices(a) return nil if a.length > self.length or a.any? {|x| x < 0 or x >= self.length} ret = [] a.each {|i| ret << self[i] } ret end