HermannHH
3/22/2018 - 3:32 PM

array_target.rb

# given - ARRAY of numeric values
# expected - NUMBER Single numerice value being looked for
##### Example problem set
# f = [10, 20 , 34]
# Determine which combination of the array items is equal to 44
# Usage: target_finder(f, 44)
# Output: The subset of population records that will equal 44 is [10, 34]
def target_finder(given, expected)
  combos = (1..given.size).each_with_object([]) { |i, arr|
    given.combination(i).each { |c| arr << c if c.reduce(:+) == expected}
  }
  print "The subset of population records that will equal #{expected.to_s} is #{combos.flatten.to_s}"
end