1

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

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

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}
4

1 回答 1

0

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

您如何看待这个解决方案?(单个参数必须是数组的哈希,就像你的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!使用而修改了输入对象。

于 2012-11-08T08:35:32.477 回答