【问题标题】:How to generate all possible combinations如何生成所有可能的组合
【发布时间】:2015-11-18 20:17:29
【问题描述】:

我有一个这样的字符串数组:

[
  "productRankingOct12:LessPopularityEPC",
  "productRankingOct12:CtrEpcJob", 
  "productRankingOct12:DeviceSpecificLPE", 
  "guidedSearch:false", 
  "guidedSearch:true", 
  "spotfront4:true", 
  "spotfront4:false",
  "postClick4:ipsa", 
  "postClick4:false",
  "trendingModule:false"
]

我必须根据给定的键值对构建 URL。我想要元素的组合:

[
  "productRankingOct12:LessPopularityEPC",
  "productRankingOct12:CtrEpcJob", 
  "productRankingOct12:DeviceSpecificLPE",
  "guidedSearch:false", 
  "guidedSearch:true", 
  "productRankingOct12:LessPopularityEPC & guidedSearch:false"
  "productRankingOct12:CtrEpcJob & guidedSearch:false"
  "productRankingOct12:DeviceSpecificLPE & guidedSearch:false"
  "productRankingOct12:LessPopularityEPC & guidedSearch:true"
  "productRankingOct12:CtrEpcJob & guidedSearch:true"
  "productRankingOct12:DeviceSpecificLPE & guidedSearch:true"
  "productRankingOct12:LessPopularityEPC & guidedSearch:false & spotfront4:true"
  "productRankingOct12:CtrEpcJob & guidedSearch:false & "spotfront4:true""
  "productRankingOct12:DeviceSpecificLPE & guidedSearch:false & "spotfront4:true""
  "productRankingOct12:LessPopularityEPC & guidedSearch:true & "spotfront4:true""
]

我尝试使用这个来生成排列:

def Automate_AB_tests.permutation(arg_keyVal)
  return [arg_keyVal] if arg_keyVal.size < 2
  ch = arg_keyVal[0] 
  permutation(arg_keyVal[1..-1]).each_with_object([]) do |perm,result|
    (0..perm.size).each {|i| result << perm.dup.insert(i,ch)}
  end
end

但它似乎正在占用所有元素,并且崩溃了。我不想置换所有元素。我想要上述所需的输出。请任何人都可以帮助我。

【问题讨论】:

    标签: ruby recursion combinations


    【解决方案1】:

    首先,我无法弄清楚为什么您的输出中到处都缺少"trendingModule:false"

    为什么输出中缺少"spotfront4:true""spotfront4:false""postClick4:ipsa""postClick4:ipsa" 等行?每个组合的包含或排除背后的逻辑是什么?

    由于键重复,您希望排除像 "key:true&amp;key:false" 这样的行。

    其次,这个程序产生了所有可能的组合,但这不是你想要的,对吧? 215 个元素,比您在示例中列出的要多得多

    opts = [
      "productRankingOct12:LessPopularityEPC",
      "productRankingOct12:CtrEpcJob", 
      "productRankingOct12:DeviceSpecificLPE", 
      "guidedSearch:false", 
      "guidedSearch:true", 
      "spotfront4:true", 
      "spotfront4:false",
      "postClick4:ipsa", 
      "postClick4:false",
      "trendingModule:false"
    ]
    
    normal_opts = Hash.new
    opts.each do |e|
      k, v = e.split(":")
      (normal_opts[k] ||= []) << v
    end
    
    # normal_ops looks like this
    #normal_opts = {
    #  "productRankingOct12" => ["LessPopularityEPC", "CtrEpcJob", "DeviceSpecificLPE"],
    #  "guidedSearch" => ["false", "true"],
    #  "spotfornt4" => ["true", "false"],
    #  "postClick4" => ["ipsa", "false"],
    #  "trendingModule" => ["false"]
    #}
    
    # nobody is ever going to mantain this
    result = normal_opts.keys.length.times.map do |i|
      normal_opts.keys.combination(i + 1).map do |keys|
        combs = keys.map { |k|
          normal_opts[k].map { |e|
            "#{k}:#{e}"
          }
        }
        # good luck understanding this!
        combs.first.product(*combs[1..-1]).map{|e| e.join("&")}
      end
    end
    
    puts result.flatten
    

    如果你能更好地解释自己,我可以改进我的答案

    【讨论】:

    • 您好,非常抱歉。其实我还没有写完所有的组合。我只是拿了两个字符串并写了所有可能的组合。但是,我必须考虑所有可能的组合......如果你能帮我解决这个问题。
    • 你为我做了很棒的工作。只是我有很多数据,所以花费的时间太长了。如果无论如何你都能提高这段代码的效率,那对我来说会很棒。
    • 鉴于问题的性质,任何解决方案都必须预期高计算复杂性。当然可以优化代码,也许你可以想出一个比现在快几个数量级的解决方案,但我觉得当你开始使用生成的数据时,无论如何你都会遇到效率问题
    猜你喜欢
    • 1970-01-01
    • 2016-01-07
    • 1970-01-01
    • 2022-01-18
    • 2022-08-22
    相关资源
    最近更新 更多