确定一个数组是否包含ruby中另一个数组的内容

在ruby中,我如何测试一个数组不仅具有另一个数组的元素,而是以特定顺序包含它们?

correct_combination = [1, 2, 3, 4, 5] [1, 5, 8, 2, 3, 4, 5].function_name(correct_combination) # => false [8, 10, 1, 2, 3, 4, 5, 9].function_name(correct_combination) # => true 

我尝试使用include ,但是用于测试[1,2,3].include?(2)是否为真。

您可以使用each_cons方法:

 arr = [1, 2, 3, 4, 5] [1, 5, 8, 2, 3, 4, 5].each_cons(arr.size).include? arr 

在这种情况下,它适用于任何元素。

我认为这可以简单地完成。

 class Array def contain? other; (self & other) == other end end correct_combination = [1, 2, 3, 4, 5] [1, 5, 8, 2, 3, 4, 5].contain?(correct_combination) # => false [8, 10, 1, 2, 3, 4, 5, 9].contain?(correct_combination) # => true 

如果你想忽略这个顺序,(就像我遇到这篇文章时那样),你可以使用Array.sort和<=> http://ruby-doc.org/core-1.8.7/classes/Array。 HTML#M000316

 a = [1, 2, 3, 4, 5] b = [2, 1, 5, 4, 3] a.sort <=> b.sort 

然后,您需要检查输出值是否等于0。

不完全是最好的解决方案,但至少它是简短的

 (',' + [1, 5, 8, 2, 3, 4, 5].join(',') + ',').include?(',' + correct_combination.join(',') + ',') 

可能的最佳解决方案是在arrays上使用字符串搜索算法之一,但您必须自己编写代码,我认为没有标准解决方案。

这就是我提出的

 a = [1, 2, 3, 4, 5] b = [2, 3, 5] c = [3, 9] irb(main):037:0* (a + b).sort.uniq == a.sort.uniq => true irb(main):038:0> (a + c).sort.uniq == a.sort.uniq => false 

我想考虑容器数组中其他数组的连续元素序列,并提出: – 代码的灵感来自Sawa的代码

 class Array def contain? other arr = self & other (arr.eql? other ) && ((self.index(arr.last) - self.index(arr.first)).eql?(other.size - 1)) end end 

结果: –

 correct_combination = [1, 2, 3, 4, 5] [1, 5, 8, 2, 3, 4, 5].contain?(correct_combination) # => false [8, 10, 1, 2, 3, 4, 5, 9].contain?(correct_combination) # => true [1, 8, 2, 3, 4, 5].contain?(correct_combination) # => false 

我建议使用for循环来比较每一个

 @out_of_order_elements = [] for i in 0.. @array_size do unless submission_array[i] == @correct_combination[i] @out_of_order_ids.push(@submission_array[i]) end end 

也许<=>是您正在寻找的。

比较 – 如果此数组小于,等于或大于other_array,则返回整数(-1,0或+1)

 a = [1, 2, 3, 4, 5] b = [1, 5, 8, 2, 3, 4, 5] c = [8, 10, 1, 2, 3, 4, 5, 9] puts a <=> b # => -1 puts a <=> c # => -1 puts a <=> a # => 0 

更新:没关系,只是注意到它并不关心位置。

 puts a <=> a.reverse # => -1 

这是我能想到的最好的。 所有的return调用都有点难看,但如果它是大型数组,它应该比进行字符串比较更快。

 class Array def same?(o) if self.size == o.size (0..self.size).each {|i| return false if self[i] != o[i] } else return false end return true end end a = [1,2,3,4,5] b = [1, 5, 8, 2, 3, 4, 5] c = [1, 2, 6, 4, 5] puts a.same?(a.reverse) # => false puts a.same?(a) # => true puts a.same?(b) # => false puts a.same?(c) # => false 

一个非常快速的方法是简单地从另一个数组中减去一个数组并测试一个空数组。

 correct_combination = [1, 2, 3, 4, 5] yep = [8, 10, 1, 2, 3, 4, 5, 9] nope = [1, 8, 2, 3, 4] if correct_combination - yep == [] puts "yep has all the values" end if correct_combination - nope == [] puts "nope has all the values" end 

这种方法并不关心位置所以删除掉!

对不起……我也错过了这个问题。 没意识到你在寻找优先顺序。 当我找到一个解决方案来评估一个大型数组是否包含另一个大型数组的所有条目时,我遇到了这个问题。 .all?/包括? 方法需要很长时间才能完成。 祝好运!

您可以简单地将数组比较为字符串:

 correct_combination = [1, 2, 3, 4, 5] yep = [8, 10, 1, 2, 3, 4, 5, 9] nope = [1, 5, 8, 2, 3, 4, 5] if yep.to_s.include?(correct_combination.to_s) puts "yep" end if nope.to_s.include?(correct_combination.to_s) puts "nope" end