Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/ruby/25.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/extjs/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ruby 重构此代码以提高可读性_Ruby_Arrays - Fatal编程技术网

Ruby 重构此代码以提高可读性

Ruby 重构此代码以提高可读性,ruby,arrays,Ruby,Arrays,请看下面的代码 def test array = Array.new array2 = Array.new groups = [[424235, "goa", "italy"], [523436, "mumbai"], [342423, "africa", "goa"]] type = ["goa", "mumbai"] groups.each_with_index do |item,index| if item.include?(type[0]) == t

请看下面的代码

def test
  array = Array.new
  array2 = Array.new

  groups = [[424235, "goa", "italy"], [523436, "mumbai"], [342423, "africa", "goa"]]
  type = ["goa", "mumbai"]
  groups.each_with_index do |item,index|

       if item.include?(type[0]) == true
         array << index  << array2
        elsif item.include?(type[1]) == true
               array2 << index 
       else
         "nothing ;)"
       end

  end
  print array.each_slice(2).map { |a, b| [a, b.first] }
end
combine

#Output - [[0, 1], [2, 1]]
def测试
array=array.new
array2=Array.new
集团=[[424235,“果阿”,“意大利”],[523436,“孟买”],[342423,“非洲”,“果阿”]]
类型=[“果阿”、“孟买”]
分组。每个_带有|索引do |项,索引|
如果项目.include?(类型[0])==true
数组这是我的代码

def combinations(groups, types)
  array = Array.new(types.size) { Array.new([]) }
  groups.each_with_index do |item, index|
     types.each_with_index { |type, i| array[i] << index if item.include? type }
  end

  flat = array.inject { |acc, i| acc.product i }.flatten
  flat.each_slice(types.size).to_a
end
输出:
[[0,1],[2,1]]

combinations([[424235, "goa", "italy"], [523436, "mumbai"], [342423, "africa", "goa"]], ["goa", "africa"])
输出:
[[0,2],[2,2]]

combinations([[424235, "goa", "italy"], [523436, "mumbai"], [342423, "africa", "goa"], [123, "india"]], ["goa", "mumbai", "india"])
输出:
[[0,1,3],[2,1,3]]

combinations([[424235, "goa", "italy"], [523436, "mumbai"], [342423, "mumbai", "goa"], [123, "india"]], ["goa", "mumbai", "india", "italy"])
输出:
[[0,1,3,0],[0,2,3,0],[2,1,3,0],[2,2,3,0]


如果我正确理解了你的问题,那么这些应该是正确的。虽然我可能误解了你。请务必告诉我,我是否把您的问题搞错了,您是否可以提供非常好的测试用例。

您在这里实际想做什么?获取组中类型的索引?是。所以我要找的是[goa]型和[mumbai]型。并将它们生成位置的索引。请注意,goa始终优先。您的输出不应该是[[0,2],[1]]。因为果阿在第0组和第2组,而孟买只在第1组?对。但它在寻找类型中可能的值与组的组合。所以是果阿,孟买(0,1)和果阿,孟买,但这次是2,1。所有可能的组合。希望我说的有道理:)不,我相信你做得对。我只需要稍微调整一下以适应我更新的案例:)。非常感谢@ahmed
combinations([[424235, "goa", "italy"], [523436, "mumbai"], [342423, "mumbai", "goa"], [123, "india"]], ["goa", "mumbai", "india", "italy"])