【问题标题】:How do I keep the hash associations when building permutations构建排列时如何保持哈希关联
【发布时间】:2012-11-08 05:56:30
【问题描述】:

更新:最后可能的解决方案,但肯定不是高性能或理想的。

我创建了以下方法,让我接近我想要的。

def multi_permutations(collection)
  case collection.length
  when 1
    return collection.shift[1]
  when 0
    raise "You must pass in a multidimensional collection."
  end

  a = collection.shift[1]
  b = multi_permutations(collection)

  return_value = []
  a.each do |a_value|
    b.each do |b_value|
      return_value << [a_value] + [b_value]
    end
  end

  return return_value
end

当我传入一个带有嵌套数组的哈希时,看起来像这样......

my_collection["item_9"]   = [152]
my_collection["item_2"]   = [139, 143, 145]
my_collection["item_13"]  = [138, 142, 150]
my_collection["item_72"]  = [137, 149, 151, 154]
my_collection["item_125"] = [140, 141]
my_collection["item_10"]  = [144, 146, 147, 148, 153]

我希望它创建一个哈希数组,所有排列看起来像这样......

[{ "item_9": 152 }, { "item_2": 139 }, { "item_13": 138 }, { "item_72": 137 }, { "item_125": 140 }, { "item_10": 144 }]
[{ "item_9": 152 }, { "item_2": 139 }, { "item_13": 138 }, { "item_72": 137 }, { "item_125": 140 }, { "item_10": 146 }]
[{ "item_9": 152 }, { "item_2": 139 }, { "item_13": 138 }, { "item_72": 137 }, { "item_125": 140 }, { "item_10": 147 }]
[{ "item_9": 152 }, { "item_2": 139 }, { "item_13": 138 }, { "item_72": 137 }, { "item_125": 140 }, { "item_10": 148 }]
[{ "item_9": 152 }, { "item_2": 139 }, { "item_13": 138 }, { "item_72": 137 }, { "item_125": 140 }, { "item_10": 153 }]
.
.
.
[{ "item_9": 152 }, { "item_2": 145 }, { "item_13": 150 }, { "item_72": 154 }, { "item_125": 141 }, { "item_10": 144 }]
[{ "item_9": 152 }, { "item_2": 145 }, { "item_13": 150 }, { "item_72": 154 }, { "item_125": 141 }, { "item_10": 146 }]
[{ "item_9": 152 }, { "item_2": 145 }, { "item_13": 150 }, { "item_72": 154 }, { "item_125": 141 }, { "item_10": 147 }]
[{ "item_9": 152 }, { "item_2": 145 }, { "item_13": 150 }, { "item_72": 154 }, { "item_125": 141 }, { "item_10": 148 }]
[{ "item_9": 152 }, { "item_2": 145 }, { "item_13": 150 }, { "item_72": 154 }, { "item_125": 141 }, { "item_10": 153 }]

这个函数最终做的很接近,但我失去了关系。

[152, [139, [138, [137, [140, 144]]]]]
[152, [139, [138, [137, [140, 146]]]]]
[152, [139, [138, [137, [140, 147]]]]]
[152, [139, [138, [137, [140, 148]]]]]
[152, [139, [138, [137, [140, 153]]]]]
.
.
.
[152, [145, [150, [154, [141, 144]]]]]
[152, [145, [150, [154, [141, 146]]]]]
[152, [145, [150, [154, [141, 147]]]]]
[152, [145, [150, [154, [141, 148]]]]]
[152, [145, [150, [154, [141, 153]]]]]

人际关系对我来说非常重要。原因是,我计划水合一个对象,其中 attrs 是哈希中的键。我确信这可以以更好的方式完成,我愿意接受建议。

所以我想出的一种可能的解决方案是创建一个键数组,然后将排列展平并将它们压缩到一个散列中。

  results = []
  permutations = multi_permutations(possibilities)
  permutations.each do |permutation|
    results << Hash[keys.zip permutation.flatten!]
  end

这最终给了我......

{"item_9"=>152, "item_2"=>145, "item_13"=>150, "item_72"=>154, "item_125"=>141, "item_10"=>146}
{"item_9"=>152, "item_2"=>145, "item_13"=>150, "item_72"=>154, "item_125"=>141, "item_10"=>147}
{"item_9"=>152, "item_2"=>145, "item_13"=>150, "item_72"=>154, "item_125"=>141, "item_10"=>148}
{"item_9"=>152, "item_2"=>145, "item_13"=>150, "item_72"=>154, "item_125"=>141, "item_10"=>153}

【问题讨论】:

    标签: ruby permutation


    【解决方案1】:

    我无法让你的解决方案运行,因为你没有提供如何调用它,所以我无法比较效率。

    您如何看待这个解决方案? (单个参数必须是数组哈希,就像你的 my_collection

    my_collection = {}
    my_collection["item_9"]   = [152]
    my_collection["item_2"]   = [139, 143, 145]
    my_collection["item_13"]  = [138, 142, 150]
    my_collection["item_72"]  = [137, 149, 151, 154]
    my_collection["item_125"] = [140, 141]
    my_collection["item_10"]  = [144, 146, 147, 148, 153]
    
    def permutations!(input)
      input.each do |key, possibilities|
        possibilities.map!{|p| {key => p} }
      end
    
      digits = input.keys.map!{|key| input[key] }
    
      digits.shift.product(*digits)
    end
    
    results = permutations!(my_collection)
    

    请注意,由于map! 的用法,此方法会修改输入对象。

    【讨论】:

    • 我用你的方法代替了我的方法并得到了这些结果... {1=>[{1=>110}], 2=>[{2=>103}, {2= >106}, {2=>107}, {2=>108}, {2=>109}], 3=>[{3=>112}], 4=>[{4=>101}], 5=>[{5=>102}, {5=>104}, {5=>105}], 6=>[{6=>111}]} 我确实有一个如何在我的问题的底部。问题在于它不会为每个排列创建不同的记录。它像我的一样将数组堆叠起来。 :(
    • 哈希输入:{1=>[110], 2=>[103, 106, 107, 108, 109], 3=>[112], 4=>[101], 5= >[102, 104, 105], 6=>[111]} 散列:{1=>[{1=>110}], 2=>[{2=>103}, {2=>106}, {2=>107}, {2=>108}, {2=>109}], 3=>[{3=>112}], 4=>[{4=>101}], 5=>[ {5=>102}、{5=>104}、{5=>105}]、6=>[{6=>111}]}
    • 我已经更新了我的答案。请与您的东西隔离运行以避免副作用。
    • 这就像我要求的那样工作。我问错了。 :( 不过谢谢!我想要像最后一节中那样的输出,我刚刚意识到我所要求的正是你给我的。给你积分以获得一个很好的解决方案。如果你有时间,我会喜欢一些帮助将格式更改为我想要的格式。即下面的格式...“这最终给了我...”
    • 无论哪种方式,我认为你让我朝着正确的方向前进。所以现在我只是想看看我是否能理解这种方法。谢谢!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-03-10
    • 1970-01-01
    • 2014-01-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多