Class: ComboData::DataSet
- Inherits:
-
Object
- Object
- ComboData::DataSet
- Defined in:
- lib/combo_data/data_set.rb
Instance Attribute Summary collapse
-
#data ⇒ Object
Returns the value of attribute data.
-
#max_combination_length ⇒ Object
Returns the value of attribute max_combination_length.
-
#precision ⇒ Object
Returns the value of attribute precision.
-
#timeout ⇒ Object
Returns the value of attribute timeout.
Instance Method Summary collapse
- #combinations_summing_to(value) ⇒ Object
-
#initialize(data = []) ⇒ DataSet
constructor
A new instance of DataSet.
- #pairs_with_a_difference_of(value) ⇒ Object
Constructor Details
#initialize(data = []) ⇒ DataSet
Returns a new instance of DataSet.
8 9 10 11 12 13 14 |
# File 'lib/combo_data/data_set.rb', line 8 def initialize(data = []) @data = data @max_combination_length = 2 @precision = 2 @timeout = 60 @search_value = nil end |
Instance Attribute Details
#data ⇒ Object
Returns the value of attribute data.
6 7 8 |
# File 'lib/combo_data/data_set.rb', line 6 def data @data end |
#max_combination_length ⇒ Object
Returns the value of attribute max_combination_length.
6 7 8 |
# File 'lib/combo_data/data_set.rb', line 6 def max_combination_length @max_combination_length end |
#precision ⇒ Object
Returns the value of attribute precision.
6 7 8 |
# File 'lib/combo_data/data_set.rb', line 6 def precision @precision end |
#timeout ⇒ Object
Returns the value of attribute timeout.
6 7 8 |
# File 'lib/combo_data/data_set.rb', line 6 def timeout @timeout end |
Instance Method Details
#combinations_summing_to(value) ⇒ Object
16 17 18 19 |
# File 'lib/combo_data/data_set.rb', line 16 def combinations_summing_to(value) @search_value = value sum_hash(@data.reject{ |v| v > @search_value}).select{ |addends, sum| sum.round(@precision) == @search_value.round(@precision) } end |
#pairs_with_a_difference_of(value) ⇒ Object
21 22 23 24 25 26 |
# File 'lib/combo_data/data_set.rb', line 21 def pairs_with_a_difference_of(value) @search_value = value Timeout.timeout(@timeout) do difference_hash(@data).select{ |minuends, difference| difference.round(@precision) == @search_value.round(@precision) } end end |