Module: Combinatorics::Derange::Mixin
- Included in:
- Array
- Defined in:
- lib/combinatorics/derange/mixin.rb
Overview
Instance Method Summary collapse
-
#derange {|derangement| ... } ⇒ Enumerator
Calculate all derangements for an Enumerable object.
Instance Method Details
#derange {|derangement| ... } ⇒ Enumerator
Calculate all derangements for an Enumerable object.
32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 |
# File 'lib/combinatorics/derange/mixin.rb', line 32 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 |