How to get the next and previous element in an array, Ruby

Pedro

I have this array and need to replace its elements by the multiplication of its previous and next.

I do the following:

   array.each_with_index{|element, index|
      next_element = array[index+1]
      previous_element = array[index-1]
    }

   array.map! {|a|
      if a == array.first
        a = a * next_element
      elsif a == array.last
        a = a * previous_element
      else
        a = next_element * previous_element
      end
      }

I expect the following result:

array = [4, 1, 6, 7, 9, 3, 0]       #given array
array = [4, 24, 7, 54, 21, 0, 0]    #array replaced

I get the following error:

undefined local variable or method `next_element' for Arrays:Class

Is there a easy way of getting the previous and next element of a given array element?

Am I using the array.map! method right?

Stefan

This would work:

array = [4, 1, 6, 7, 9, 3, 0]

[nil, *array, nil].each_cons(3).map { |l, m, r| (l || m) * (r || m) }
#=> [4, 24, 7, 54, 21, 0, 0]

The array is surrounded by nil values, so each element has neighbors. each_cons(3) then yiels each element along with its neighbors to map which multiplies the left (l) with the right (r) neighbor, falling back to the middle element (m) if one of the neighbors happens to be nil.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

How to get next and previous five element of from an array?

How to get Previous and Next element of a list in python

How to get previous and next array value in php?

Compare array element previous and next

Grabbing previous and next element in an array

How to get the second element of an array in an array ruby

How to get the next element in a sparse array?

How to get next element in a sparse array

Get the next element of an array

Get next array element

How to get previous week days and next 2 weeks in array?

How to get previous and next date compared to an array of dates in PHP

Javascript forEach() through an array: how to get the previous and next item?

Previous or next element in array based on string "direction"

Next array element must start off with previous

Ember.js previous/next element of an array

Get next / previous element using JavaScript?

How to get the index of element in an array and return the next element in Hive?

Iterator has .next() - is there a way to get the previous element instead of the next one?

Ruby Array - reverse iterate with next and before element

php how to get next element in the array and loop again when ended?

How can I get the next element of an array in C?

How to get previous element in a LinkedList?

How to get Previous Element with BeautifulSoup

Expanding an array by storing the next element right after the end of the previous array

php how to navigate to previous and next value in an array

How to attach the previous item to the next in array?

Given an element in a sequence, how to get the previous element?

How to get next/previous double records in MySQL?