Advertisement
Guest User

Untitled

a guest
Apr 26th, 2015
188
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.19 KB | None | 0 0
  1. def get_best_profit_brute_force(stock_prices_yesterday):
  2.  
  3. max_profit = 0
  4.  
  5. # go through every time
  6. for outer_time in xrange(len(stock_prices_yesterday)):
  7.  
  8. # for every time, go through every OTHER time
  9. for inner_time in xrange(len(stock_prices_yesterday)):
  10.  
  11. # for each pair, find the earlier and later times
  12. earlier_time = min(outer_time, inner_time)
  13. later_time = max(outer_time, inner_time)
  14.  
  15. # and use those to find the earlier and later prices
  16. earlier_price = stock_prices_yesterday[earlier_time]
  17. later_price = stock_prices_yesterday[later_time]
  18.  
  19. # see what our profit would be if we bought at the
  20. # earlier price and sold at the later price
  21. potential_profit = later_price - earlier_price
  22.  
  23. # update max_profit if we can do better
  24. max_profit = max(max_profit, potential_profit)
  25.  
  26. return max_profit
  27.  
  28. def get_best_profit_smarter_brute_force(stock_prices_yesterday):
  29.  
  30. max_profit = 0
  31.  
  32. # go through every price (with it's index as the time)
  33. for earlier_time, earlier_price in enumerate(stock_prices_yesterday):
  34.  
  35. # and go through all the LATER prices
  36. for later_price in stock_prices_yesterday[earlier_time:]:
  37.  
  38. # see what our profit would be if we bought at the
  39. # earlier price and sold at the later price
  40. potential_profit = later_price - earlier_price
  41.  
  42. # update max_profit if we can do better
  43. max_profit = max(max_profit, potential_profit)
  44.  
  45. return max_profit
  46.  
  47. def get_best_profit_positive(stock_prices_yesterday):
  48.  
  49. min_price = stock_prices_yesterday[0]
  50. max_profit = 0
  51.  
  52. for current_price in stock_prices_yesterday:
  53.  
  54. # ensure min_price is the lowest price we've seen so far
  55. min_price = min(min_price, current_price)
  56.  
  57. # see what our profit would be if we bought at the
  58. # min price and sold at the current price
  59. potential_profit = current_price - min_price
  60.  
  61. # update max_profit if we can do better
  62. max_profit = max(max_profit, potential_profit)
  63.  
  64. return max_profit
  65.  
  66. def get_best_profit(stock_prices_yesterday):
  67.  
  68. # make sure we have at least 2 prices
  69. if len(stock_prices_yesterday) < 2:
  70. raise IndexError('Getting a profit requires at least 2 prices')
  71.  
  72. # we'll greedily update min_price and max_profit, so we initialize
  73. # them to the first price and the first possible profit
  74. min_price = stock_prices_yesterday[0]
  75. max_profit = stock_prices_yesterday[1] - stock_prices_yesterday[0]
  76.  
  77. for index, current_price in enumerate(stock_prices_yesterday):
  78.  
  79. # skip the first time, since we already used it
  80. # when we initialized min_price and max_profit
  81. if index == 0:
  82. continue
  83.  
  84. # see what our profit would be if we bought at the
  85. # min price and sold at the current price
  86. potential_profit = current_price - min_price
  87.  
  88. # update max_profit if we can do better
  89. max_profit = max(max_profit, potential_profit)
  90.  
  91. # update min_price so it's always
  92. # the lowest price we've seen so far
  93. min_price = min(min_price, current_price)
  94.  
  95. return max_profit
  96.  
  97.  
  98. # tests
  99.  
  100. functions = [
  101. get_best_profit_brute_force,
  102. get_best_profit_smarter_brute_force,
  103. get_best_profit_positive,
  104. get_best_profit
  105. ]
  106.  
  107. def test(function, input, output):
  108. assert function(input) == output
  109.  
  110. positive_tests = [
  111. ([10, 20, 5], 10), # simple buy and sell
  112. ([10, 5, 10, 20], 15), # wait to buy and sell
  113. ([10, 10, 10], 0), # no change
  114. ]
  115.  
  116. negative_tests = [
  117. ([35, 20, 10], -10), # decrease in value all day
  118. ([30, 20, 10], -10), # steady decrease
  119. ([100, 70, 50], -20), # decrease, wait to buy
  120. ]
  121.  
  122. for function in functions:
  123. for input, output in positive_tests:
  124. test(function, input, output)
  125.  
  126. for input, output in negative_tests:
  127. test(get_best_profit, input, output)
  128.  
  129. import unittest
  130.  
  131. class EnsureErrors(unittest.TestCase):
  132.  
  133. def test(self):
  134. self.assertRaises(IndexError, get_best_profit, [1])
  135. self.assertRaises(IndexError, get_best_profit, [])
  136.  
  137. if __name__ == '__main__':
  138. unittest.main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement