Skip to content

Instantly share code, notes, and snippets.

@tjsingleton
Forked from martin2110/apple_stock.rb
Last active October 6, 2015 20:32

Revisions

  1. tjsingleton revised this gist Oct 6, 2015. 1 changed file with 8 additions and 6 deletions.
    14 changes: 8 additions & 6 deletions apple_stock.rb
    Original file line number Diff line number Diff line change
    @@ -1,17 +1,19 @@
    # flip condition
    # 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 |buy_price, buy_index|
    stock.each_with_index do |sell_price, sell_index|
    next unless buy_index < sell_index
    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]
  2. tjsingleton revised this gist Oct 6, 2015. 1 changed file with 2 additions and 2 deletions.
    4 changes: 2 additions & 2 deletions apple_stock.rb
    Original file line number Diff line number Diff line change
    @@ -1,4 +1,4 @@
    # replace if with guard
    # 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 unless best_value < value
    next if value < best_value

    best_value = value
    best_buy_index = buy_index
  3. tjsingleton revised this gist Oct 6, 2015. 1 changed file with 6 additions and 6 deletions.
    12 changes: 6 additions & 6 deletions apple_stock.rb
    Original file line number Diff line number Diff line change
    @@ -1,4 +1,4 @@
    # only use hash for result -- reduces objects allocated and cost of hash lookups for value
    # 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
    if best_value < value
    best_value = value
    best_buy_index = buy_index
    best_sell_index = sell_index
    end
    next unless best_value < value

    best_value = value
    best_buy_index = buy_index
    best_sell_index = sell_index
    end
    end

  4. tjsingleton revised this gist Oct 6, 2015. 1 changed file with 12 additions and 6 deletions.
    18 changes: 12 additions & 6 deletions apple_stock.rb
    Original file line number Diff line number Diff line change
    @@ -1,22 +1,28 @@
    # Only keep track of the best
    # 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_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|
    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
    if best_value < value
    best_value = value
    best_buy_index = buy_index
    best_sell_index = sell_index
    end
    end
    end

    best
    {value: best_value, buy_index: best_buy_index, sell_index: best_sell_index}
    end

    def test_picking_best_time_to_buy
  5. tjsingleton revised this gist Oct 6, 2015. 1 changed file with 9 additions and 10 deletions.
    19 changes: 9 additions & 10 deletions apple_stock.rb
    Original file line number Diff line number Diff line change
    @@ -1,23 +1,22 @@
    # Use array literal and format pick_best_time_to_buy
    # Only keep track of the best

    require "pp"
    require "minitest/autorun"

    class StockTest < Minitest::Test
    def pick_best_time_to_buy(stock)
    prices = []

    best = {value: 0}
    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
    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

    max = prices.max_by{|k,v| k[:value] }
    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
    end
  6. tjsingleton revised this gist Oct 6, 2015. 1 changed file with 5 additions and 2 deletions.
    7 changes: 5 additions & 2 deletions apple_stock.rb
    Original file line number Diff line number Diff line change
    @@ -1,10 +1,12 @@
    # Extract Method
    # 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 = Array.new
    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

  7. tjsingleton revised this gist Oct 6, 2015. 1 changed file with 9 additions and 4 deletions.
    13 changes: 9 additions & 4 deletions apple_stock.rb
    Original file line number Diff line number Diff line change
    @@ -1,10 +1,9 @@
    # Convert to unit test
    # Extract Method

    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]

    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]
  8. tjsingleton revised this gist Oct 6, 2015. 1 changed file with 22 additions and 11 deletions.
    33 changes: 22 additions & 11 deletions apple_stock.rb
    Original file line number Diff line number Diff line change
    @@ -1,13 +1,24 @@
    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
    # 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
    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]}"
  9. @martin2110 martin2110 revised this gist Oct 6, 2015. No changes.
  10. @martin2110 martin2110 created this gist Oct 6, 2015.
    13 changes: 13 additions & 0 deletions apple_stock.rb
    Original 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]}"