Compare commits
	
		
			5 Commits
		
	
	
		
			d511103596
			...
			9a2b0e4ee9
		
	
	| Author | SHA1 | Date | |
|---|---|---|---|
|   | 9a2b0e4ee9 | ||
| c161b1f7c1 | |||
| 1c0c5c6153 | |||
| 7f12fcba18 | |||
|   | b3918e333e | 
							
								
								
									
										2
									
								
								Gemfile
									
									
									
									
									
								
							
							
						
						
									
										2
									
								
								Gemfile
									
									
									
									
									
								
							| @ -1,6 +1,6 @@ | |||||||
| source "https://rubygems.org" | source "https://rubygems.org" | ||||||
| 
 | 
 | ||||||
| ruby "3.2.0" | ruby "3.3.4" | ||||||
| 
 | 
 | ||||||
| # Bundle edge Rails instead: gem "rails", github: "rails/rails", branch: "main" | # Bundle edge Rails instead: gem "rails", github: "rails/rails", branch: "main" | ||||||
| gem "rails", "~> 7.1.3", ">= 7.1.3.2" | gem "rails", "~> 7.1.3", ">= 7.1.3.2" | ||||||
|  | |||||||
| @ -99,7 +99,8 @@ GEM | |||||||
|     factory_bot_rails (6.4.3) |     factory_bot_rails (6.4.3) | ||||||
|       factory_bot (~> 6.4) |       factory_bot (~> 6.4) | ||||||
|       railties (>= 5.0.0) |       railties (>= 5.0.0) | ||||||
|     faker (3.1.1) |     faker (3.4.2) | ||||||
|  |       i18n (>= 1.8.11, < 2) | ||||||
|     globalid (1.2.1) |     globalid (1.2.1) | ||||||
|       activesupport (>= 6.1) |       activesupport (>= 6.1) | ||||||
|     i18n (1.14.5) |     i18n (1.14.5) | ||||||
| @ -288,7 +289,7 @@ DEPENDENCIES | |||||||
|   web-console |   web-console | ||||||
| 
 | 
 | ||||||
| RUBY VERSION | RUBY VERSION | ||||||
|    ruby 3.2.0p0 |    ruby 3.3.4p94 | ||||||
| 
 | 
 | ||||||
| BUNDLED WITH | BUNDLED WITH | ||||||
|    2.5.6 |    2.5.6 | ||||||
|  | |||||||
| @ -13,7 +13,7 @@ module Tables | |||||||
|     def random_distribution(people) |     def random_distribution(people) | ||||||
|       @tables = [] |       @tables = [] | ||||||
| 
 | 
 | ||||||
|       @tables << people.slice!(0..rand(@min_per_table..@max_per_table)) while people.any? |       @tables << Table.new(people.slice!(0..rand(@min_per_table..@max_per_table))) while people.any? | ||||||
|     end |     end | ||||||
| 
 | 
 | ||||||
|     def discomfort |     def discomfort | ||||||
| @ -59,7 +59,7 @@ module Tables | |||||||
|     private |     private | ||||||
| 
 | 
 | ||||||
|     def local_discomfort(table) |     def local_discomfort(table) | ||||||
|       DiscomfortCalculator.new(table).calculate |       table.discomfort ||= DiscomfortCalculator.new(table).calculate | ||||||
|     end |     end | ||||||
|   end |   end | ||||||
| end | end | ||||||
|  | |||||||
| @ -8,6 +8,9 @@ module Tables | |||||||
|     def each |     def each | ||||||
|       @initial_solution.tables.combination(2) do |table_a, table_b| |       @initial_solution.tables.combination(2) do |table_a, table_b| | ||||||
|         table_a.product(table_b).each do |(person_a, person_b)| |         table_a.product(table_b).each do |(person_a, person_b)| | ||||||
|  |           original_discomfort_a = table_a.reset | ||||||
|  |           original_discomfort_b = table_b.reset | ||||||
|  | 
 | ||||||
|           table_a.delete(person_a) |           table_a.delete(person_a) | ||||||
|           table_b.delete(person_b) |           table_b.delete(person_b) | ||||||
| 
 | 
 | ||||||
| @ -21,6 +24,9 @@ module Tables | |||||||
| 
 | 
 | ||||||
|           table_a << person_a |           table_a << person_a | ||||||
|           table_b << person_b |           table_b << person_b | ||||||
|  | 
 | ||||||
|  |           table_a.discomfort = original_discomfort_a | ||||||
|  |           table_b.discomfort = original_discomfort_b | ||||||
|         end |         end | ||||||
|       end |       end | ||||||
|     end |     end | ||||||
|  | |||||||
							
								
								
									
										15
									
								
								app/services/tables/table.rb
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										15
									
								
								app/services/tables/table.rb
									
									
									
									
									
										Normal file
									
								
							| @ -0,0 +1,15 @@ | |||||||
|  | module Tables | ||||||
|  |   class Table < Array | ||||||
|  |     attr_accessor :discomfort | ||||||
|  |     def initialize(*args) | ||||||
|  |       super | ||||||
|  |       reset | ||||||
|  |     end | ||||||
|  | 
 | ||||||
|  |     def reset | ||||||
|  |       original_discomfort = discomfort | ||||||
|  |       @discomfort = nil | ||||||
|  |       original_discomfort | ||||||
|  |     end | ||||||
|  |   end | ||||||
|  | end | ||||||
| @ -3,3 +3,9 @@ class Numeric | |||||||
|     Money.from_amount(self, "EUR").format |     Money.from_amount(self, "EUR").format | ||||||
|   end |   end | ||||||
| end | end | ||||||
|  | 
 | ||||||
|  | class Array | ||||||
|  |   def to_table | ||||||
|  |     Tables::Table.new(self) | ||||||
|  |   end | ||||||
|  | end | ||||||
| @ -14,8 +14,8 @@ module Tables | |||||||
|       context 'when there are two tables with two people each' do |       context 'when there are two tables with two people each' do | ||||||
|         let(:initial_solution) do |         let(:initial_solution) do | ||||||
|           Distribution.new(min_per_table: 2, max_per_table: 2).tap do |distribution| |           Distribution.new(min_per_table: 2, max_per_table: 2).tap do |distribution| | ||||||
|             distribution.tables << %i[a b] |             distribution.tables << %i[a b].to_table | ||||||
|             distribution.tables << %i[c d] |             distribution.tables << %i[c d].to_table | ||||||
|           end |           end | ||||||
|         end |         end | ||||||
| 
 | 
 | ||||||
| @ -32,8 +32,8 @@ module Tables | |||||||
|       context 'when there are two tables with three people each' do |       context 'when there are two tables with three people each' do | ||||||
|         let(:initial_solution) do |         let(:initial_solution) do | ||||||
|           Distribution.new(min_per_table: 3, max_per_table: 3).tap do |distribution| |           Distribution.new(min_per_table: 3, max_per_table: 3).tap do |distribution| | ||||||
|             distribution.tables << %i[a b c] |             distribution.tables << %i[a b c].to_table | ||||||
|             distribution.tables << %i[d e f] |             distribution.tables << %i[d e f].to_table | ||||||
|           end |           end | ||||||
|         end |         end | ||||||
| 
 | 
 | ||||||
| @ -55,9 +55,9 @@ module Tables | |||||||
|       context 'when there are three tables with two people each' do |       context 'when there are three tables with two people each' do | ||||||
|         let(:initial_solution) do |         let(:initial_solution) do | ||||||
|           Distribution.new(min_per_table: 2, max_per_table: 2).tap do |distribution| |           Distribution.new(min_per_table: 2, max_per_table: 2).tap do |distribution| | ||||||
|             distribution.tables << %i[a b] |             distribution.tables << %i[a b].to_table | ||||||
|             distribution.tables << %i[c d] |             distribution.tables << %i[c d].to_table | ||||||
|             distribution.tables << %i[e f] |             distribution.tables << %i[e f].to_table | ||||||
|           end |           end | ||||||
|         end |         end | ||||||
| 
 | 
 | ||||||
|  | |||||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user