在构建排列时如何保持哈希关联

时间:2012-11-08 05:56:31

标签: ruby permutation

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

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

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}

1 个答案:

答案 0 :(得分:0)

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

您对此解决方案有何看法? (单个参数必须是一个Hash of Arrays,就像你的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!用法,此方法会修改输入对象。