Class: Array

Inherits:
Object
  • Object
show all
Defined in:
lib/one_factorization/array.rb

Instance Method Summary collapse

Instance Method Details

#one_factorizeObject



2
3
4
5
6
7
8
9
10
11
12
# File 'lib/one_factorization/array.rb', line 2

def one_factorize
  number_pairs = length / 2
  first, *rest = *self
  result = rest.each_with_index.inject([]) do |set, (person,index)|
    set << (1..number_pairs-1).inject([[first, person]]) do |pairs, offset|
      pairs << [rest[(index-offset)%rest.length], rest[(index+offset)%rest.length]]
    end
  end
  fail OneFactorization::InvalidLengthError.new(result) if length.odd?
  result
end