Define a WheelSwap class that randomly swaps one guest from each table
This commit is contained in:
parent
e8a88b50e2
commit
a1f06dae5b
@ -10,7 +10,12 @@ module Tables
|
|||||||
end
|
end
|
||||||
|
|
||||||
def call
|
def call
|
||||||
@initial_solution.deep_dup
|
new_solution = @initial_solution.deep_dup
|
||||||
|
|
||||||
|
selected_guests = new_solution.tables.map(&:pop).cycle.tap(&:next)
|
||||||
|
new_solution.tables.each { |table| table << selected_guests.next }
|
||||||
|
|
||||||
|
new_solution
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
@ -10,4 +10,10 @@ class Set
|
|||||||
def to_table
|
def to_table
|
||||||
Tables::Table.new(self)
|
Tables::Table.new(self)
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def pop
|
||||||
|
element = self.to_a.sample
|
||||||
|
self.delete(element)
|
||||||
|
element
|
||||||
|
end
|
||||||
end
|
end
|
@ -15,6 +15,13 @@ module Tables
|
|||||||
result = described_class.new(initial_solution).call
|
result = described_class.new(initial_solution).call
|
||||||
|
|
||||||
expect(result.tables.size).to eq(3)
|
expect(result.tables.size).to eq(3)
|
||||||
|
expect(result.tables.map(&:size)).to all(eq(3))
|
||||||
|
|
||||||
|
expect(result.tables).not_to include(initial_solution.tables[0])
|
||||||
|
expect(result.tables).not_to include(initial_solution.tables[1])
|
||||||
|
expect(result.tables).not_to include(initial_solution.tables[2])
|
||||||
|
|
||||||
|
expect(result.tables.map(&:to_a).flatten).to contain_exactly(*(:a..:i).to_a)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
Loading…
x
Reference in New Issue
Block a user