建立排列时如何保持杂凑关联 [英] How do I keep the hash associations when building permutations
问题描述
更新:最终可能的解决方案,但肯定不是理想的或理想的.
UPDATE: Possible solution at the end, but certainly not performant or ideal.
我创建了以下方法,使我更接近想要的东西.
I created the following method that gives me close to what I want.
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
当我传递带有嵌套数组的散列时,看起来像这样...
When I pass in a hash with nested arrays that looks like this...
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]
我希望它创建具有所有排列的散列数组,看起来像这样……
I want it to create an array of hashes with all permutations that looks like this...
[{ "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 }]
此功能最终完成的工作很接近,但我失去了联系.
What this function ends up doing is close but I lose the relationships.
[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是哈希中的键的对象水合.我相信这可以通过更好的方式来完成,并且我愿意提出建议.
The relationships are very important to me. The reason is, I plan to hydrate an object where the attrs are the keys in the hash. I'm sure this can be done in a better way, and I'm open to suggestions.
所以我想出的一种可能的解决方案是创建一个键数组,然后展平排列并将它们压缩在一起成为哈希.
So one possible solution I've come up with is to create a keys array then flatten the permutations and zip them together in a hash.
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}
推荐答案
我无法运行您的解决方案,因为您没有提供调用方法,因此我无法比较效率.
I couldn't get your solution to run because you did not provide how to call it, so I couldn't compare the efficiency.
您如何看待该解决方案? (就像您的my_collection
一样,单个参数必须是数组的哈希)
What do you think about this solution? (the single argument has to be a Hash of Arrays, just like your 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!
,此方法会修改输入对象.
Please notice that this method modifies the input object because of the map!
usage.
这篇关于建立排列时如何保持杂凑关联的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!