Skip to content

Instantly share code, notes, and snippets.

@edelbalso
Last active January 4, 2016 01:49
Show Gist options
  • Save edelbalso/8551397 to your computer and use it in GitHub Desktop.
Save edelbalso/8551397 to your computer and use it in GitHub Desktop.
An algorithm for finding all permutations of possible values among 5 variables.
# assumes a hash collection's .each preserves order (true in ruby 1.9+ I believe)
inputs = {
param1: [70,90,130],
param2: [150_000, 170_000, 190_000],
param3: [260, 370, 90],
param4: [12,17,19],
param5: ['string1','string2'],
}
arr1 = [[]] # algorithm only works if arr1 can be iterated over at least once
arr2 = []
inputs.each do |key,values|
arr1.each do |possible_permutations|
values.each do |value|
arr2 << (possible_permutations.clone << value)
end
end
arr1 = arr2
arr2 = []
end
puts arr1.map{ |arr|
{
param1: arr[0],
param2: arr[1],
param3: arr[2],
param4: arr[3],
param5: arr[4],
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment