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
13
14
15
16
# File 'lib/one_factorization/array.rb', line 2

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