-
-
Save ncaron/1125b59c8969810fe8d9844db926f842 to your computer and use it in GitHub Desktop.
Stock Picker solution from The Odin Project
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 prices | |
best_days = [] | |
profit = 0 | |
i = 0 | |
while i < prices.length - 1 | |
j = i | |
while j < prices.length | |
possible_profit = prices[j] - prices[i] | |
if possible_profit > profit | |
profit = possible_profit | |
best_days = [i, j] | |
end | |
j += 1 | |
end | |
i += 1 | |
end | |
best_days | |
end | |
p stock_picker([17,3,6,9,15,8,6,1,10]) == [1, 4] | |
p stock_picker([1, 15]) == [0, 1] | |
p stock_picker([15, 1]) == [] | |
p stock_picker([15, 1, 2, 3]) == [1, 3] | |
p stock_picker([15, 2, 2, 3]) == [1, 3] | |
p stock_picker([15, 3, 2, 3]) == [2, 3] |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment