Ruby Stock Picker
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def stock_picker(stocks) | |
profits = {} | |
stocks.each_with_index do |price_i, i| | |
stocks.each_with_index do |price_j, j| | |
profits[[i, j]] = price_j - price_i if j > i | |
end | |
end | |
index, _ = profits.max_by { |k, v| v } | |
return index | |
end | |
p stock_picker([17,3,6,9,15,8,6,1,10]) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment