22

What I have:

Let's say I have a hash like this, with various values belonging to one parameter.

a = {}
a[:bitrate] = ["100", "500", "1000"]
a[:fps] = ["15", "30"]
a[:qp] = ["20", "30"]

What I need:

I need some way to iteratively get all the possible combinations of these values, so, with all the parameter/value pairs:

  • bitrate = 100, fps = 15, qp = 20
  • bitrate = 500, fps = 15, qp = 30
  • ...

The number of parameters (i.e. the keys) and the number of values (i.e. the length of the value arrays) are not known beforehand. Ideally, I'd do something like:

a.foo do |ret|
  puts ret.keys   # => ["bitrate", "fps", "qp"]
  puts ret.values # => ["100", "15", "20"]
end

… where the block is called for each possible combination. How can I define foo?


What I (probably) don't need:

Now, I know this: Combine array of array into all possible combinations, forward only, in Ruby, suggesting something like:

a.first.product(*a[1..-1]).map(&:join)

But this operates on values and arrays in arrays only, and I need the original reference to the parameter's name.

Community
  • 1
  • 1
slhck
  • 36,575
  • 28
  • 148
  • 201

4 Answers4

32
a = {}
a[:bitrate] = ["100", "500", "1000"]
a[:fps] = ["15", "30"]
a[:qp] = ["20", "30"]

def product_hash(hsh)
  attrs   = hsh.values
  keys    = hsh.keys
  product = attrs[0].product(*attrs[1..-1])
  product.map{ |p| Hash[keys.zip p] }
end

product_hash(a)

you'll get

[{:bitrate=>"100", :fps=>"15", :qp=>"20"},
 {:bitrate=>"100", :fps=>"15", :qp=>"30"},
 {:bitrate=>"100", :fps=>"30", :qp=>"20"},
 {:bitrate=>"100", :fps=>"30", :qp=>"30"},
 {:bitrate=>"500", :fps=>"15", :qp=>"20"},
 {:bitrate=>"500", :fps=>"15", :qp=>"30"},
 {:bitrate=>"500", :fps=>"30", :qp=>"20"},
 {:bitrate=>"500", :fps=>"30", :qp=>"30"},
 {:bitrate=>"1000", :fps=>"15", :qp=>"20"},
 {:bitrate=>"1000", :fps=>"15", :qp=>"30"},
 {:bitrate=>"1000", :fps=>"30", :qp=>"20"},
 {:bitrate=>"1000", :fps=>"30", :qp=>"30"}]

You can also add new key to your hash.

a = {}
a[:bitrate] = ["100", "500", "1000"]
a[:fps] = ["15", "30"]
a[:qp] = ["20", "30"]
a[:bw] = [true, false]

product_hash(a)

#=>
[{:bitrate=>"100", :fps=>"15", :qp=>"20", :bw=>true},
 {:bitrate=>"100", :fps=>"15", :qp=>"20", :bw=>false},
 {:bitrate=>"100", :fps=>"15", :qp=>"30", :bw=>true},
 {:bitrate=>"100", :fps=>"15", :qp=>"30", :bw=>false},
 {:bitrate=>"100", :fps=>"30", :qp=>"20", :bw=>true},
 {:bitrate=>"100", :fps=>"30", :qp=>"20", :bw=>false},
 {:bitrate=>"100", :fps=>"30", :qp=>"30", :bw=>true},
 {:bitrate=>"100", :fps=>"30", :qp=>"30", :bw=>false},
 {:bitrate=>"500", :fps=>"15", :qp=>"20", :bw=>true},
 {:bitrate=>"500", :fps=>"15", :qp=>"20", :bw=>false},
 {:bitrate=>"500", :fps=>"15", :qp=>"30", :bw=>true},
 {:bitrate=>"500", :fps=>"15", :qp=>"30", :bw=>false},
 {:bitrate=>"500", :fps=>"30", :qp=>"20", :bw=>true},
 {:bitrate=>"500", :fps=>"30", :qp=>"20", :bw=>false},
 {:bitrate=>"500", :fps=>"30", :qp=>"30", :bw=>true},
 {:bitrate=>"500", :fps=>"30", :qp=>"30", :bw=>false},
 {:bitrate=>"1000", :fps=>"15", :qp=>"20", :bw=>true},
 {:bitrate=>"1000", :fps=>"15", :qp=>"20", :bw=>false},
 {:bitrate=>"1000", :fps=>"15", :qp=>"30", :bw=>true},
 {:bitrate=>"1000", :fps=>"15", :qp=>"30", :bw=>false},
 {:bitrate=>"1000", :fps=>"30", :qp=>"20", :bw=>true},
 {:bitrate=>"1000", :fps=>"30", :qp=>"20", :bw=>false},
 {:bitrate=>"1000", :fps=>"30", :qp=>"30", :bw=>true},
 {:bitrate=>"1000", :fps=>"30", :qp=>"30", :bw=>false}]
fl00r
  • 82,987
  • 33
  • 217
  • 237
2

Just FYI I took fl00r's approach and monkey-patched it. I like it a bit better.

class Hash
  def product
    product = values[0].product(*values[1..-1])
    product.map{|p| Hash[keys.zip p]}
  end
end
Raj
  • 1,764
  • 2
  • 13
  • 14
1

Please try OCG options combination generator.

require "ocg"

generator = OCG.new(
  :bitrate => %w[100 500 1000],
  :fps => %w[15 30],
  :qp => %w[20 30]
)

puts generator.next until generator.finished?

Generator includes much more functionality that will help you to deal with other options.

puchu
  • 3,294
  • 6
  • 38
  • 62
0

I believe fl00r's answer is almost perfect but has a drawback. It assumes that hsh.values and hsh.keys will have a matching order, which as far as I know, is not warrantied. So you will probably need an extra step to ensure that. Maybe something like:

def product_hash(hsh)
  keys  = hsh.keys
  attrs = keys.map { |key| hsh[key] }
  product = attrs[0].product(*attrs[1..-1])
  product.map{ |p| Hash[keys.zip p] }
end

But fl00r can correct me if I'm wrong.

Fabian N.
  • 3,807
  • 2
  • 23
  • 46
  • For Ruby >= 1.9 they are in the same order – fl00r May 23 '17 at 17:41
  • Is that warrantied by the language specification or something that only applies to the MRI? In both cases, can you please point me to the documentation ensuring that? I never know where to search for such stuff. Thanks. – Ancor Gonzalez Sosa Jun 02 '17 at 11:06
  • http://ruby-doc.org/core-2.4.1/Hash.html `Hashes enumerate their values in the order that the corresponding keys were inserted.` – fl00r Jun 02 '17 at 12:12