-
-
Save tjsingleton/942a1f80ef29e4b84df4 to your computer and use it in GitHub Desktop.
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
# Only keep track of the best | |
require "pp" | |
require "minitest/autorun" | |
class StockTest < Minitest::Test | |
def pick_best_time_to_buy(stock) | |
best = {value: 0} | |
stock.each_with_index do |buy_price, buy_index| | |
stock.each_with_index do |sell_price, sell_index| | |
next unless buy_index < sell_index | |
value = sell_price - buy_price | |
best = {sell_index: sell_index, buy_index: buy_index, value: value} if best[:value] < value | |
end | |
end | |
best | |
end | |
def test_picking_best_time_to_buy | |
stock = [1,8,3,0,1,54,29,1,3,10,49,10,39,28,41,1,3] | |
max = pick_best_time_to_buy(stock) | |
assert_equal 3, max[:buy_index] | |
assert_equal 5, max[:sell_index] | |
assert_equal 54, max[:value] | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment