Ruby 如何在两个数组之间获取最大值数组
我正在寻找一种优雅的方法来获取包含两个数组之间最大值的数组 这意味着如果有两个数组:Ruby 如何在两个数组之间获取最大值数组,ruby,Ruby,我正在寻找一种优雅的方法来获取包含两个数组之间最大值的数组 这意味着如果有两个数组: a = [1, 5, 9] b = [3, 2, 11] 结果应该是: => [3, 5, 11] 假设两个数组的大小相同 我使用的代码感觉不像是一种Ruby方式来完成该任务: c = Array.new(a.size) for i in 0...a.size c[i] = [a[i], b[i]].max end 这应该起作用: [a, b].transpose.map(&:max)
a = [1, 5, 9]
b = [3, 2, 11]
结果应该是:
=> [3, 5, 11]
假设两个数组的大小相同
我使用的代码感觉不像是一种Ruby方式来完成该任务:
c = Array.new(a.size)
for i in 0...a.size
c[i] = [a[i], b[i]].max
end
这应该起作用:
[a, b].transpose.map(&:max)
#=> [3, 5, 11]
transpose
返回[[1,3]、[5,2]、[9,11]
和map(&:max)
查找每个子数组的最大值
a.zip(b)
(正如Abe Voelker所建议的那样)等同于[a,b]。如果两个数组的元素数相同,则进行转置。如果元素大小不同,转置
将引发异常:
[1].zip([2,3])
#=> [[1,2]]
[[1], [2,3]].transpose
#=> IndexError: element size differs
基准
输出
user system total real
transpose 0.430000 0.000000 0.430000 ( 0.428760)
zip 0.420000 0.000000 0.420000 ( 0.415070)
lazy.zip 1.010000 0.000000 1.010000 ( 1.009173)
loop (max) 0.490000 0.000000 0.490000 ( 0.489015)
loop (>?:) 0.150000 0.000000 0.150000 ( 0.151461)
下面的情况如何
注意:大小应等于两个数组
a = [1, 5, 9]
b = [3, 2, 11]
p a.size.times.map{|i| [a[i],b[i]].max}
# >> [3, 5, 11]
或
或者
基准
require 'benchmark'
iterations = 10_000
a = [1, 5, 9]
b = [3, 2,11]
def stefan(a,b)
[a, b].transpose.map(&:max)
end
def abe(a,b)
a.zip(b).map(&:max)
end
def babai1(a,b)
a.size.times.map{|i| a[i]>b[i] ? a[i] : b[i] }
end
def babai2(a,b)
a.size.times.map{|i| [a[i],b[i]].max}
end
def babai3(a,b)
a.each_index.map{|i| a[i]>b[i] ? a[i] : b[i] }
end
Benchmark.bm do |bm|
bm.report('Stefan') do
iterations.times do
stefan(a,b)
end
end
bm.report('Abe') do
iterations.times do
abe(a,b)
end
end
bm.report('babai1') do
iterations.times do
babai1(a,b)
end
end
bm.report('babai2') do
iterations.times do
babai2(a,b)
end
end
bm.report('babai3') do
iterations.times do
babai3(a,b)
end
end
end
输出
user system total real
Stefan 0.047000 0.000000 0.047000 ( 0.046874)
Abe 0.047000 0.000000 0.047000 ( 0.046873)
babai1 0.031000 0.000000 0.031000 ( 0.031249)
babai2 0.062000 0.000000 0.062000 ( 0.062497)
babai3 0.032000 0.000000 0.032000 ( 0.031249)
为什么不是9,5?两个数组之间的最大值逻辑是什么?@Babai这是元素最大值为什么不显示您编写的代码,让我们知道您试图自己找到答案?这是堆栈溢出方式。“询问代码的问题必须表明对正在解决的问题的最低理解。包括尝试的解决方案、它们不起作用的原因以及预期的结果。另请参见:”。但是,使用[a,b]。转置您不能像使用zip那样使用懒惰的枚举器。对于大型阵列,这将创建大量临时阵列。@stefan您现在可以进行基准测试了吗?你是这方面的专家,我知道:)为了将来的参考,我只想指出,当我发布我的答案时,你的答案中没有关于zip
的信息;你的忍者编辑没有出现在编辑历史记录中:-)(只是不想让ppl认为我复制了它)@AbeVoelker是的,所以没有跟踪每一个更改,我已经添加了一个对你文章的引用。在Ruby 2.0下,你可能想做一个懒惰的zip,a.lazy.zip(b).map(&:max).to_a
,以便通过大量数组获得可能的内存增益(尽管速度降低了约2倍)
a = [1, 5, 9]
b = [3, 2,11]
p a.size.times.map{|i| a[i]>b[i] ? a[i] : b[i] }
# >> [3, 5, 11]
a = [1, 5, 9]
b = [3, 2, 11]
p a.each_index.map{|i| a[i]>b[i] ? a[i] : b[i] }
# >> [3, 5, 11]
require 'benchmark'
iterations = 10_000
a = [1, 5, 9]
b = [3, 2,11]
def stefan(a,b)
[a, b].transpose.map(&:max)
end
def abe(a,b)
a.zip(b).map(&:max)
end
def babai1(a,b)
a.size.times.map{|i| a[i]>b[i] ? a[i] : b[i] }
end
def babai2(a,b)
a.size.times.map{|i| [a[i],b[i]].max}
end
def babai3(a,b)
a.each_index.map{|i| a[i]>b[i] ? a[i] : b[i] }
end
Benchmark.bm do |bm|
bm.report('Stefan') do
iterations.times do
stefan(a,b)
end
end
bm.report('Abe') do
iterations.times do
abe(a,b)
end
end
bm.report('babai1') do
iterations.times do
babai1(a,b)
end
end
bm.report('babai2') do
iterations.times do
babai2(a,b)
end
end
bm.report('babai3') do
iterations.times do
babai3(a,b)
end
end
end
user system total real
Stefan 0.047000 0.000000 0.047000 ( 0.046874)
Abe 0.047000 0.000000 0.047000 ( 0.046873)
babai1 0.031000 0.000000 0.031000 ( 0.031249)
babai2 0.062000 0.000000 0.062000 ( 0.062497)
babai3 0.032000 0.000000 0.032000 ( 0.031249)