What's the efficient way to multiply two arrays and get sum of multiplied values in Ruby?

前端 未结 9 915
猫巷女王i
猫巷女王i 2021-02-09 12:54

What\'s the efficient way to multiply two arrays and get sum of multiply values in Ruby? I have two arrays in Ruby:

array_A = [1, 2, 1, 4, 5, 3, 2, 6, 5, 8, 9]
a         


        
9条回答
  •  情话喂你
    2021-02-09 13:19

    Walking through each element should be a must

    (0...array_A.count).inject(0) {|r, i| r + array_A[i]*array_B[i]}
    

提交回复
热议问题