Home > Blockchain >  How can I loop over an array and deleting the first element after each iteration in Ruby?
How can I loop over an array and deleting the first element after each iteration in Ruby?

Time:10-28

I was working on a project on theodinproject ruby curriculum. I am to implement a method that takes in an array of stock prices and return the best day to buy and the best day to sell. The index of each price (array element) is its day. The way I want to approach the problem was to first copy the array to a new binding, then map over the original array and inside it maps into the copied array and deleting the first element after the loop finished i.e., to make for the case whereby a person cannot buy in day 10 and sell in day 1.

This is the code I've written:

def stock_picker(stock_price)
  #copying the array into a new binding
  stock_array = []
  stock_array.replace(stock_price)

  stock_price.map do |buy|
    stock_array.map do |sell|
      sell - buy
    end

    #deleting the first element
    stock_array.shift
  end
end

array = stock_picker([17,3,6,9,15,8,6,1,10])

This was result I received:

[17, 3, 6, 9, 15, 8, 6, 1, 10]

and this is what I'm expecting to get:

[[0, -14, -11, -8, -2, -9, -11, -16, -7], [0, 3, 6, 12, 5, 3, -2, 7], [0, 3, 9, 2, 0, -5, 4], [0, 6, -1, -3, -8, 1], [0, -7, -9, -14, -5], [0, -2, -7, 2], [0, -5, 4], [0, 9], [0]]

CodePudding user response:

First of all, I don't understand why you want to shift the first element each time. But here are some remarks to help you on your way:

  • In a .map block, the last value gets returned. Which in this case is stock_array.shift. That is why you get your initial array as a result. Cause it iterates over the array and each time returns the first element but since you shift the array each time you just go to the next value, and the next one and so on...

  • Instead of having a new array and replacing it with stock_price, you could simple duplicating it with stock_price.dup

You'd have something like this:

def stock_picker(stock_price)
  stock_price.dup.map do |buy|
    res = stock_price.map do |sell|
      sell - buy
    end

    stock_price.shift
    res
  end
end

But again, the above example returns what you wanted, I don't understand the why...

CodePudding user response:

Here is the simple loop method to get your expected output:

Code:

array = [17,3,6,9,15,8,6,1,10]
arr = [17,3,6,9,15,8,6,1,10]
answer = []

for i in array 
    temp = []
    arr.each do |x| 
      temp.push(x - i)
  end 
      arr.shift
  answer.push(temp)
end 

print answer

will give

Output:

[[0, -14, -11, -8, -2, -9, -11, -16, -7], [0, 3, 6, 12, 5, 3, -2, 7], [0, 3, 9, 2, 0, -5, 4], [0, 6, -1, -3, -8, 1], [0, -7, -9, -14, -5], [0, -2, -7, 2], [0, -5, 4], [0, 9], [0]]

CodePudding user response:

Try with following

def stock_picker(stock_price)
  #copying the array into a new binding
  stock_array = []
  stock_array.replace(stock_price)
  
  stock_price.map do |buy|
    result = stock_array.map {|sell| sell - buy }
    stock_array.shift
    p result
  end
end

array = stock_picker([17,3,6,9,15,8,6,1,10])

Output

[0, -14, -11, -8, -2, -9, -11, -16, -7]
[0, 3, 6, 12, 5, 3, -2, 7]
[0, 3, 9, 2, 0, -5, 4]
[0, 6, -1, -3, -8, 1]
[0, -7, -9, -14, -5]
[0, -2, -7, 2]
[0, -5, 4]
[0, 9]
[0]
  • Related