Define a WheelSwap class that randomly swaps one guest from each table

This commit is contained in:
bustikiller 2025-07-23 10:22:41 +02:00
parent e8a88b50e2
commit a1f06dae5b
3 changed files with 19 additions and 1 deletions

View File

@ -10,7 +10,12 @@ module Tables
end
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

View File

@ -10,4 +10,10 @@ class Set
def to_table
Tables::Table.new(self)
end
def pop
element = self.to_a.sample
self.delete(element)
element
end
end

View File

@ -15,6 +15,13 @@ module Tables
result = described_class.new(initial_solution).call
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