1 Replies - 2333 Views - Last Post: 29 March 2012 - 03:18 AM Rate Topic: -----

#1 tetroel  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 4
  • Joined: 24-March 12

I have NO idea of how to do this task. Couldn't you hint at it?

Posted 29 March 2012 - 02:57 AM

So, i have an array of 30 elements, each of them can take the value from 0 to 1000
This part of the code is easy and i know how to write it. The complicated part is next: i need to write a program that returns the biggest value of the sum of the two adjacent elements.(a[i]+a[i+1])
For example:
a= [1,2,1,4,3]
a[0]+a[1]=3
a[1]+a[2]=3
a[2]+a[3]=5
a[3]+a[4]=7 # so it is what i'm searching for, 7 is bigger than 3, 5.
____
The thing is that i actually don't realize what methods/iterators/else? to use
.find_all doesn't seem to do what i need
.inject has the same trouble
so, what can you advice me?
thank you in advance

Is This A Good Question/Topic? 0
  • +

Replies To: I have NO idea of how to do this task. Couldn't you hint at it?

#2 sepp2k  Icon User is online

  • D.I.C Lover
  • member icon

Reputation: 2100
  • View blog
  • Posts: 3,197
  • Joined: 21-June 11

Re: I have NO idea of how to do this task. Couldn't you hint at it?

Posted 29 March 2012 - 03:18 AM

You can use the method each_cons(2) to get an enumerable containing two-element arrays for each 2 consecutive elements. So for example [1,2,3,4].each_cons(2) will give you the two-element arrays [1,2], [2,3], and [3,4]. From there you hopefully know how to continue.
Was This Post Helpful? 1
  • +
  • -

Page 1 of 1