Created
December 9, 2015 19:39
-
-
Save edenthecat/0fe55c5523ab2e46aeaa to your computer and use it in GitHub Desktop.
Ruby Exercise: https://www.interviewcake.com/question/stock-price
This file contains hidden or 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 getMaxProfit(stock_prices_yesterday) | |
if stock_prices_yesterday.index(stock_prices_yesterday.min) < stock_prices_yesterday.index(stock_prices_yesterday.max) | |
max_profit = (stock_prices_yesterday.max - stock_prices_yesterday.min) | |
else | |
possible_profits = [] | |
minIndex = 0 | |
maxIndex = stock_prices_yesterday.length | |
stock_prices_yesterday[0...(maxIndex-1)].each do |i| | |
stock_prices_yesterday[minIndex...maxIndex].each do |j| | |
possible_profits.push(j - i) | |
end | |
minIndex += 1 | |
end | |
max_profit = possible_profits.max | |
end | |
puts "max profit: " + max_profit.to_s | |
end | |
getMaxProfit([11, 10, 9, 8, 7]) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
For some reason it's not showing negative results for when the prices go down all day..