是否有一个算法,从阵列中提取二重奏价值观,工作呢? [英] Is there an algorithm to extract values in duets from an array and operate over them?
本文介绍了是否有一个算法,从阵列中提取二重奏价值观,工作呢?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有这样一个数组:
[1,2,3,4,5,6,7,8,9]
和我想在这样的二重奏项目:
- 1,2
- 在做一些操作...
- 2,3
- 在做一些操作...
- 3,4
- 在做一些操作...
- 4,5
- 在做一些操作...
- 5,6
- 在做一些操作...
- 6,7
- 在做一些操作...
- 7,8
- 在做一些操作...
- 8,9
- 在做一些操作...
请提出一个优雅的方式来实现这一目标使用Ruby。
解决方案
[1,2,3,4,5,6,7,8,9] .each_cons 2做| A,B |
P [A,B]
结束
I have an array like:
[1,2,3,4,5,6,7,8,9]
And I want to get items in duets like this:
- 1,2
- Do some operations...
- 2,3
- Do some operations...
- 3,4
- Do some operations...
- 4,5
- Do some operations...
- 5,6
- Do some operations...
- 6,7
- Do some operations...
- 7,8
- Do some operations...
- 8,9
- Do some operations...
Please suggest an elegant way to achieve this using Ruby.
解决方案
[1,2,3,4,5,6,7,8,9].each_cons 2 do |a,b|
p [a,b]
end
这篇关于是否有一个算法,从阵列中提取二重奏价值观,工作呢?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文