From 29d9d219164103f6a1edcbdd3a1e0455e5b3596c Mon Sep 17 00:00:00 2001 From: Manuel Bustillo Date: Sun, 10 Nov 2024 17:30:01 +0100 Subject: [PATCH] Use sets instead of arrays to represent tables --- app/services/tables/swap.rb | 2 +- app/services/tables/table.rb | 2 +- config/initializers/ruby_extensions.rb | 2 +- spec/services/tables/swap_spec.rb | 64 +++++++++++++------------- 4 files changed, 35 insertions(+), 35 deletions(-) diff --git a/app/services/tables/swap.rb b/app/services/tables/swap.rb index 9668efb..ae74294 100644 --- a/app/services/tables/swap.rb +++ b/app/services/tables/swap.rb @@ -9,7 +9,7 @@ module Tables def each @initial_solution.tables.combination(2) do |table_a, table_b| - table_a.product(table_b).each do |(person_a, person_b)| + table_a.to_a.product(table_b.to_a).each do |(person_a, person_b)| original_discomfort_a = table_a.reset original_discomfort_b = table_b.reset diff --git a/app/services/tables/table.rb b/app/services/tables/table.rb index 8f58854..2c50435 100644 --- a/app/services/tables/table.rb +++ b/app/services/tables/table.rb @@ -1,7 +1,7 @@ # Copyright (C) 2024 Manuel Bustillo module Tables - class Table < Array + class Table < Set attr_accessor :discomfort, :min_per_table, :max_per_table def initialize(*args) diff --git a/config/initializers/ruby_extensions.rb b/config/initializers/ruby_extensions.rb index e515905..46016e7 100644 --- a/config/initializers/ruby_extensions.rb +++ b/config/initializers/ruby_extensions.rb @@ -6,7 +6,7 @@ class Numeric end end -class Array +class Set def to_table Tables::Table.new(self) end diff --git a/spec/services/tables/swap_spec.rb b/spec/services/tables/swap_spec.rb index 3022af2..159d998 100644 --- a/spec/services/tables/swap_spec.rb +++ b/spec/services/tables/swap_spec.rb @@ -16,17 +16,17 @@ module Tables context 'when there are two tables with two people each' do let(:initial_solution) do Distribution.new(min_per_table: 2, max_per_table: 2).tap do |distribution| - distribution.tables << %i[a b].to_table - distribution.tables << %i[c d].to_table + distribution.tables << Set[:a, :b].to_table + distribution.tables << Set[:c, :d].to_table end end it 'yields all possible swaps between the tables' do expect(swaps).to contain_exactly( - [%i[a d], %i[c b]], - [%i[b c], %i[d a]], - [%i[a c], %i[d b]], - [%i[b d], %i[c a]] + [Set[:a, :d], Set[:c, :b]], + [Set[:b, :c], Set[:d, :a]], + [Set[:a, :c], Set[:d, :b]], + [Set[:b, :d], Set[:c, :a]] ) end end @@ -34,22 +34,22 @@ module Tables context 'when there are two tables with three people each' do let(:initial_solution) do Distribution.new(min_per_table: 3, max_per_table: 3).tap do |distribution| - distribution.tables << %i[a b c].to_table - distribution.tables << %i[d e f].to_table + distribution.tables << Set[:a, :b, :c].to_table + distribution.tables << Set[:d, :e, :f].to_table end end it 'yields all possible swaps between the tables' do expect(swaps).to contain_exactly( - [%i[b c d], %i[e f a]], - [%i[b c e], %i[f d a]], - [%i[b c f], %i[d e a]], - [%i[c a d], %i[e f b]], - [%i[c a e], %i[f d b]], - [%i[c a f], %i[d e b]], - [%i[a b d], %i[e f c]], - [%i[a b e], %i[f d c]], - [%i[a b f], %i[d e c]] + [Set[:b, :c, :d], Set[:e, :f, :a]], + [Set[:b, :c, :e], Set[:f, :d, :a]], + [Set[:b, :c, :f], Set[:d, :e, :a]], + [Set[:c, :a, :d], Set[:e, :f, :b]], + [Set[:c, :a, :e], Set[:f, :d, :b]], + [Set[:c, :a, :f], Set[:d, :e, :b]], + [Set[:a, :b, :d], Set[:e, :f, :c]], + [Set[:a, :b, :e], Set[:f, :d, :c]], + [Set[:a, :b, :f], Set[:d, :e, :c]] ) end end @@ -57,26 +57,26 @@ module Tables context 'when there are three tables with two people each' do let(:initial_solution) do Distribution.new(min_per_table: 2, max_per_table: 2).tap do |distribution| - distribution.tables << %i[a b].to_table - distribution.tables << %i[c d].to_table - distribution.tables << %i[e f].to_table + distribution.tables << Set[:a, :b].to_table + distribution.tables << Set[:c, :d].to_table + distribution.tables << Set[:e, :f].to_table end end it 'yields all possible swaps between the tables' do expect(swaps).to contain_exactly( - [%i[b c], %i[d a], %i[e f]], - [%i[b d], %i[c a], %i[e f]], - [%i[a c], %i[d b], %i[e f]], - [%i[a d], %i[c b], %i[e f]], - [%i[b e], %i[c d], %i[f a]], - [%i[b f], %i[c d], %i[e a]], - [%i[a e], %i[c d], %i[f b]], - [%i[a f], %i[c d], %i[e b]], - [%i[a b], %i[d e], %i[f c]], - [%i[a b], %i[d f], %i[e c]], - [%i[a b], %i[c e], %i[f d]], - [%i[a b], %i[c f], %i[e d]] + [Set[:b, :c], Set[:d, :a], Set[:e, :f]], + [Set[:b, :d], Set[:c, :a], Set[:e, :f]], + [Set[:a, :c], Set[:d, :b], Set[:e, :f]], + [Set[:a, :d], Set[:c, :b], Set[:e, :f]], + [Set[:b, :e], Set[:c, :d], Set[:f, :a]], + [Set[:b, :f], Set[:c, :d], Set[:e, :a]], + [Set[:a, :e], Set[:c, :d], Set[:f, :b]], + [Set[:a, :f], Set[:c, :d], Set[:e, :b]], + [Set[:a, :b], Set[:d, :e], Set[:f, :c]], + [Set[:a, :b], Set[:d, :f], Set[:e, :c]], + [Set[:a, :b], Set[:c, :e], Set[:f, :d]], + [Set[:a, :b], Set[:c, :f], Set[:e, :d]] ) end end -- 2.47.1