Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save MichaelXavier/1214355 to your computer and use it in GitHub Desktop.
Save MichaelXavier/1214355 to your computer and use it in GitHub Desktop.

Revisions

  1. MichaelXavier revised this gist Sep 13, 2011. 1 changed file with 2 additions and 2 deletions.
    4 changes: 2 additions & 2 deletions hash_of_arrays_permutations.rb
    Original 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 = first.map {|x| [x]}
    rest.inject(init) {|acc, opts| permute_merge(acc, opts)}
    init = fst.map {|x| [x]}
    rst.inject(init) {|acc, opts| permute_merge(acc, opts)}
    end

    def permute_merge(acc, to_merge)
  2. MichaelXavier revised this gist Sep 13, 2011. 1 changed file with 18 additions and 10 deletions.
    28 changes: 18 additions & 10 deletions hash_of_arrays_permutations.rb
    Original 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(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
    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
    end
    acc
    }.flatten(1)
    end
    end
  3. @dplummer dplummer revised this gist Sep 13, 2011. 1 changed file with 2 additions and 6 deletions.
    8 changes: 2 additions & 6 deletions hash_of_arrays_permutations.rb
    Original 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
    permute([], 0, {})
    end

    def permute(acc, choices_index, chosen)
    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 = permute(acc, choices_index + 1, chosen)
    acc = permutations_of_arrays(acc, choices_index + 1, chosen)
    else
    acc << chosen.dup
    end
  4. @dplummer dplummer created this gist Sep 13, 2011.
    51 changes: 51 additions & 0 deletions hash_of_arrays_permutations.rb
    Original 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