Forked from dplummer/hash_of_arrays_permutations.rb
Created
September 13, 2011 17:10
-
-
Save MichaelXavier/1214355 to your computer and use it in GitHub Desktop.
Revisions
-
MichaelXavier revised this gist
Sep 13, 2011 . 1 changed file with 2 additions and 2 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -41,8 +41,8 @@ def permutations_of_arrays private def permute(arrs) fst, rst = arrs[0], arrs[1..-1] init = fst.map {|x| [x]} rst.inject(init) {|acc, opts| permute_merge(acc, opts)} end def permute_merge(acc, to_merge) -
MichaelXavier revised this gist
Sep 13, 2011 . 1 changed file with 18 additions and 10 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -32,16 +32,24 @@ # {"sizes"=>"extra-large", "color"=>"green", "style"=>"longsleeve"}] class Hash def permutations_of_arrays permute(map {|key, vals| vals.map {|val| [key, val]} }).map {|pairs| Hash[pairs]} end private def permute(arrs) fst, rst = arrs[0], arrs[1..-1] init = first.map {|x| [x]} rest.inject(init) {|acc, opts| permute_merge(acc, opts)} end def permute_merge(acc, to_merge) acc.map {|acc_arr| to_merge.map do |to_merge_el| acc_arr + [to_merge_el] end }.flatten(1) end end -
dplummer revised this gist
Sep 13, 2011 . 1 changed file with 2 additions and 6 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -32,16 +32,12 @@ # {"sizes"=>"extra-large", "color"=>"green", "style"=>"longsleeve"}] class Hash def permutations_of_arrays(acc = [], choices_index = 0, chosen = {}) key, values = sort[choices_index] values.each do |val| chosen[key] = val if choices_index + 1 < count acc = permutations_of_arrays(acc, choices_index + 1, chosen) else acc << chosen.dup end -
dplummer created this gist
Sep 13, 2011 .There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,51 @@ # Input: # choices = { # 'color' => %w(blue red green), # 'sizes' => %w(small medium large extra-large), # 'style' => %w(tshirt longsleeve) # } # # Output: # [{"sizes"=>"small", "color"=>"blue", "style"=>"tshirt"}, # {"sizes"=>"small", "color"=>"blue", "style"=>"longsleeve"}, # {"sizes"=>"medium", "color"=>"blue", "style"=>"tshirt"}, # {"sizes"=>"medium", "color"=>"blue", "style"=>"longsleeve"}, # {"sizes"=>"large", "color"=>"blue", "style"=>"tshirt"}, # {"sizes"=>"large", "color"=>"blue", "style"=>"longsleeve"}, # {"sizes"=>"extra-large", "color"=>"blue", "style"=>"tshirt"}, # {"sizes"=>"extra-large", "color"=>"blue", "style"=>"longsleeve"}, # {"sizes"=>"small", "color"=>"red", "style"=>"tshirt"}, # {"sizes"=>"small", "color"=>"red", "style"=>"longsleeve"}, # {"sizes"=>"medium", "color"=>"red", "style"=>"tshirt"}, # {"sizes"=>"medium", "color"=>"red", "style"=>"longsleeve"}, # {"sizes"=>"large", "color"=>"red", "style"=>"tshirt"}, # {"sizes"=>"large", "color"=>"red", "style"=>"longsleeve"}, # {"sizes"=>"extra-large", "color"=>"red", "style"=>"tshirt"}, # {"sizes"=>"extra-large", "color"=>"red", "style"=>"longsleeve"}, # {"sizes"=>"small", "color"=>"green", "style"=>"tshirt"}, # {"sizes"=>"small", "color"=>"green", "style"=>"longsleeve"}, # {"sizes"=>"medium", "color"=>"green", "style"=>"tshirt"}, # {"sizes"=>"medium", "color"=>"green", "style"=>"longsleeve"}, # {"sizes"=>"large", "color"=>"green", "style"=>"tshirt"}, # {"sizes"=>"large", "color"=>"green", "style"=>"longsleeve"}, # {"sizes"=>"extra-large", "color"=>"green", "style"=>"tshirt"}, # {"sizes"=>"extra-large", "color"=>"green", "style"=>"longsleeve"}] class Hash def permutations_of_arrays permute([], 0, {}) end def permute(acc, choices_index, chosen) key, values = sort[choices_index] values.each do |val| chosen[key] = val if choices_index + 1 < count acc = permute(acc, choices_index + 1, chosen) else acc << chosen.dup end end acc end end