I was wondering how you can sum from one point of an array to another point, for example:
array = [1,3,5,6,1]
assuming i is a moving variable, for example
i = 2
assuming k is a moving variable, for example
k = 4
The solution I'm looking for is to sum from array[2] to array[4], so 5+6+1 = 12
I've tried using (array[i]..array[k]).reduce(:+) but it doesn't seem to work.
Any help is appreciated!
Almost! But you're creating a range between two integer array values when you want to be indexing an array!
array[i..k].reduce(:+)
이 기사는 인터넷에서 수집됩니다. 재 인쇄 할 때 출처를 알려주십시오.
침해가 발생한 경우 연락 주시기 바랍니다[email protected] 삭제
몇 마디 만하겠습니다