Revisions
-
tjsingleton revised this gist
Oct 6, 2015 . 1 changed file with 8 additions and 6 deletions.There are no files selected for viewing
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 charactersOriginal file line number Diff line number Diff line change @@ -1,17 +1,19 @@ # swap loops so we can exit early from the inner loop once we've searched the valid range require "pp" require "minitest/autorun" class StockTest < Minitest::Test attr_accessor :loops def pick_best_time_to_buy(stock) best_value = 0 best_buy_index = 0 best_sell_index = 0 stock.each_with_index do |sell_price, sell_index| stock.each_with_index do |buy_price, buy_index| break if buy_index >= sell_index value = sell_price - buy_price next if value < best_value @@ -27,9 +29,9 @@ def pick_best_time_to_buy(stock) 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] -
tjsingleton revised this gist
Oct 6, 2015 . 1 changed file with 2 additions and 2 deletions.There are no files selected for viewing
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 charactersOriginal file line number Diff line number Diff line change @@ -1,4 +1,4 @@ # flip condition require "pp" require "minitest/autorun" @@ -14,7 +14,7 @@ def pick_best_time_to_buy(stock) next unless buy_index < sell_index value = sell_price - buy_price next if value < best_value best_value = value best_buy_index = buy_index -
tjsingleton revised this gist
Oct 6, 2015 . 1 changed file with 6 additions and 6 deletions.There are no files selected for viewing
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 charactersOriginal file line number Diff line number Diff line change @@ -1,4 +1,4 @@ # replace if with guard require "pp" require "minitest/autorun" @@ -14,11 +14,11 @@ def pick_best_time_to_buy(stock) next unless buy_index < sell_index value = sell_price - buy_price next unless best_value < value best_value = value best_buy_index = buy_index best_sell_index = sell_index end end -
tjsingleton revised this gist
Oct 6, 2015 . 1 changed file with 12 additions and 6 deletions.There are no files selected for viewing
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 charactersOriginal file line number Diff line number Diff line change @@ -1,22 +1,28 @@ # only use hash for result -- reduces objects allocated and cost of hash lookups for value require "pp" require "minitest/autorun" class StockTest < Minitest::Test def pick_best_time_to_buy(stock) best_value = 0 best_buy_index = 0 best_sell_index = 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 if best_value < value best_value = value best_buy_index = buy_index best_sell_index = sell_index end end end {value: best_value, buy_index: best_buy_index, sell_index: best_sell_index} end def test_picking_best_time_to_buy -
tjsingleton revised this gist
Oct 6, 2015 . 1 changed file with 9 additions and 10 deletions.There are no files selected for viewing
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 charactersOriginal file line number Diff line number Diff line change @@ -1,23 +1,22 @@ # 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 @@ -28,5 +27,5 @@ def test_picking_best_time_to_buy assert_equal 3, max[:buy_index] assert_equal 5, max[:sell_index] assert_equal 54, max[:value] end end -
tjsingleton revised this gist
Oct 6, 2015 . 1 changed file with 5 additions and 2 deletions.There are no files selected for viewing
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 charactersOriginal file line number Diff line number Diff line change @@ -1,10 +1,12 @@ # Use array literal and format pick_best_time_to_buy require "pp" require "minitest/autorun" class StockTest < Minitest::Test def pick_best_time_to_buy(stock) prices = [] stock.each_with_index do |buy_price, buy_index| stock.each_with_index do |sell_price, sell_index| if buy_index < sell_index @@ -14,6 +16,7 @@ def pick_best_time_to_buy(stock) end end end max = prices.max_by{|k,v| k[:value] } end -
tjsingleton revised this gist
Oct 6, 2015 . 1 changed file with 9 additions and 4 deletions.There are no files selected for viewing
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 charactersOriginal file line number Diff line number Diff line change @@ -1,10 +1,9 @@ # Extract Method require "minitest/autorun" class StockTest < Minitest::Test def pick_best_time_to_buy(stock) prices = Array.new stock.each_with_index do |buy_price, buy_index| stock.each_with_index do |sell_price, sell_index| @@ -16,6 +15,12 @@ def test_picking_best_time_to_buy end end max = prices.max_by{|k,v| k[:value] } 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] -
tjsingleton revised this gist
Oct 6, 2015 . 1 changed file with 22 additions and 11 deletions.There are no files selected for viewing
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 charactersOriginal file line number Diff line number Diff line change @@ -1,13 +1,24 @@ # Convert to unit test require "minitest/autorun" class StockTest < Minitest::Test 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] prices = Array.new stock.each_with_index do |buy_price, buy_index| stock.each_with_index do |sell_price, sell_index| if buy_index < sell_index value = sell_price - buy_price hr = {sell_index: sell_index, buy_index: buy_index, value: value} prices << hr end end end max = prices.max_by{|k,v| k[:value] } assert_equal 3, max[:buy_index] assert_equal 5, max[:sell_index] assert_equal 54, max[:value] end end -
martin2110 revised this gist
Oct 6, 2015 . No changes.There are no files selected for viewing
-
martin2110 created this gist
Oct 6, 2015 .There are no files selected for viewing
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 charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,13 @@ stock = [1,8,3,0,1,54,29,1,3,10,49,10,39,28,41,1,3] prices= Array.new stock.each_with_index do |buy_price, buy_index| stock.each_with_index do |sell_price, sell_index| if buy_index < sell_index value = sell_price - buy_price hr = {sell_index: sell_index, buy_index: buy_index, value: value} prices << hr end end end max = prices.max_by{|k,v| k[:value] } puts "Best time to buy was index #{max[:buy_index]} and the best time to sell was index #{max[:sell_index]} total profit was #{max[:value]}"