说我正在寻找2组 n 数字中的较大者(为了举例),我有这个算法:
def maxofarrays set1 set2
greater_array = []
set1.each_index do |index|
if set1[index] > set2[index] then greater_array << set1[index]
else greater_array << set2[index]
end
greater_array
end
两条最里面的代码行是否有快捷方式?或者我必须输入它吗?
答案 0 :(得分:6)
a = [347, 163, 436, 234, 113]
b = [213, 566, 124, 212, 963]
c = a.zip(b).map(&:max)
#=> [347, 566, 436, 234, 963]