如何检查一个数组的所有元素是否大于并行数组中的对应元素(在Ruby中)。

时间:2022-04-26 06:56:50

I am trying to compare two arrays to ensure that the corresponding values of one is always greater than the other.

我试图比较两个数组,以确保一个的相应值总是大于另一个。

a = [2, 3, 4]
b = [1, 2, 3]

# a[0] > b[0] ... a[x] > b[x]

At this point I am thinking of using inject with an index and return if the comparison fails, like:

此时我正在考虑使用带索引的inject并在比较失败时返回,如:

b.each_with_index.inject(true) do |cmp, (element,index)|
  if element > a[index] do
    cmp = false
    return
  end
end

Is there a better way of doing this? Kinda feeling like Ruby or Rails might already have something like this built-in and I missed it.

有更好的方法吗?有点像Ruby或Rails的感觉可能已经有类似内置的东西,我错过了它。

2 个解决方案

#1


8  

This is what I would do:

这就是我要做的:

 a.zip(b).all? { |a, b| a > b }

Note though that zip will truncate in case the two arrays are not of the same size.

请注意,如果两个数组的大小不同,则zip会截断。

#2


5  

If it's safe to assume both arrays are of the same size, here's a method that'll keep memory usage and running time to a minimum:

如果可以安全地假设两个阵列的大小相同,那么这是一种将内存使用和运行时间降至最低的方法:

(0...a.length).all?{ |i| a[i] > b[i] }
 #=> true

while also being extendable to an arbitrary number of arrays:

同时也可以扩展到任意数量的数组:

(0...a.length).all?{ |i| a[i] > [b[i], c[i], d[i]].max }

To illustrate the relative resource-intensity of the zip versus the range approach, take arrays a and b, each with length n = 5_000.

为了说明zip与范围方法的相对资源强度,取数组a和b,每个数组的长度为n = 5_000。

Here's the best case, where a[0] < b[0] is false:

这是最好的情况,其中[0]

             user     system      total        real
zip:     0.350000   0.000000   0.350000 (  0.351115)
range:   0.000000   0.000000   0.000000 (  0.000509)

and the worst, where only a[n-1] > b[n-1] is false:

最糟糕的是,只有[n-1]> b [n-1]是假的:

             user     system      total        real
zip:     0.760000   0.000000   0.760000 (  0.752424)
range:   0.420000   0.000000   0.420000 (  0.421132)

[Here's the benchmark script]

[这是基准脚本]

zip creates a new array out of the two (or more) you pass into it, which gets expensive for large n.

zip会在您传入的两个(或更多)中创建一个新数组,这对于大n来说会很昂贵。

That said, the zip approach is easier to read and more idiomatic Ruby, so if scaling isn't a concern I would use that one.

也就是说,zip方法更容易阅读和更惯用的Ruby,所以如果缩放不是一个问题我会使用那个。

#1


8  

This is what I would do:

这就是我要做的:

 a.zip(b).all? { |a, b| a > b }

Note though that zip will truncate in case the two arrays are not of the same size.

请注意,如果两个数组的大小不同,则zip会截断。

#2


5  

If it's safe to assume both arrays are of the same size, here's a method that'll keep memory usage and running time to a minimum:

如果可以安全地假设两个阵列的大小相同,那么这是一种将内存使用和运行时间降至最低的方法:

(0...a.length).all?{ |i| a[i] > b[i] }
 #=> true

while also being extendable to an arbitrary number of arrays:

同时也可以扩展到任意数量的数组:

(0...a.length).all?{ |i| a[i] > [b[i], c[i], d[i]].max }

To illustrate the relative resource-intensity of the zip versus the range approach, take arrays a and b, each with length n = 5_000.

为了说明zip与范围方法的相对资源强度,取数组a和b,每个数组的长度为n = 5_000。

Here's the best case, where a[0] < b[0] is false:

这是最好的情况,其中[0]

             user     system      total        real
zip:     0.350000   0.000000   0.350000 (  0.351115)
range:   0.000000   0.000000   0.000000 (  0.000509)

and the worst, where only a[n-1] > b[n-1] is false:

最糟糕的是,只有[n-1]> b [n-1]是假的:

             user     system      total        real
zip:     0.760000   0.000000   0.760000 (  0.752424)
range:   0.420000   0.000000   0.420000 (  0.421132)

[Here's the benchmark script]

[这是基准脚本]

zip creates a new array out of the two (or more) you pass into it, which gets expensive for large n.

zip会在您传入的两个(或更多)中创建一个新数组,这对于大n来说会很昂贵。

That said, the zip approach is easier to read and more idiomatic Ruby, so if scaling isn't a concern I would use that one.

也就是说,zip方法更容易阅读和更惯用的Ruby,所以如果缩放不是一个问题我会使用那个。