Sha256: 64a74e8963dae4ba7c4aa9304ed32c8613bbd29922663d73ea4d90fb81185e59
Contents?: true
Size: 1.21 KB
Versions: 3
Compression:
Stored size: 1.21 KB
Contents
module Combinatorics module Derange # # @author duper <super@manson.vistech.net> # # @since 0.4.0 # module Mixin # # Calculate all derangements for an Enumerable object. # # @yield [derangement] # If a block is given, it will be passed an Array representing # an individual derangement from the full calculation. # # @yieldparam [Array] derangement # One of the calculated derangements. # # @return [Enumerator] # If no block is given, an Enumerator of all derangements will be # returned. # # @example Produce the derangements of a three-element Array # [1, 2, 3].derange.to_a # # => [[2, 3, 1], [3, 1, 2]] # # @see http://en.wikipedia.org/wiki/Derangements # @see http://mathworld.wolfram.com/Derangement.html # def derange return enum_for(:derange) unless block_given? if size <= 1 yield [] else elements = self.to_a elements.permutation do |x| unless elements.each_with_index.any? { |e,i| e == x[i] } yield x end end end end end end end
Version data entries
3 entries across 3 versions & 1 rubygems
Version | Path |
---|---|
combinatorics-0.4.4 | lib/combinatorics/derange/mixin.rb |
combinatorics-0.4.3 | lib/combinatorics/derange/mixin.rb |
combinatorics-0.4.1 | lib/combinatorics/derange/mixin.rb |