Find centralized, trusted content and collaborate around the technologies you use most.
Teams
Q&A for work
Connect and share knowledge within a single location that is structured and easy to search.
我有这个数组:
a1 = [1,2,3,4]
我想从以下位置生成这个数组a1:
a1
a2 = [3, 5, 7]
公式是[a1[0] + a1[1], a1[1] + a1[2], ...]。
[a1[0] + a1[1], a1[1] + a1[2], ...]
Ruby 的方法是什么?
是的,您可以按以下方式执行此操作:
a1 = [1,2,3,4] a2 = a1.each_cons(2).map{ |a| a.inject(:+) } #=> [3, 5, 7]