Created
February 25, 2021 08:08
-
-
Save MBM1607/a5e10b9d03983d8474f6870045597d8b to your computer and use it in GitHub Desktop.
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