01010010011101010101

Untitled

Sep 20th, 2021 (edited)
85
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.28 KB | None | 0 0
  1. from itertools import combinations
  2. from random import randint
  3.  
  4.  
  5. def get_max_profit1(stock_prices):
  6.     size, result = len(stock_prices), 0
  7.     for index1, index2 in combinations(range(size), r=2):
  8.         difference = stock_prices[index2] - stock_prices[index1]
  9.         result = max(result, difference)
  10.     return result
  11.  
  12.  
  13. def get_max_profit2(stock_prices):
  14.     size, result = len(stock_prices), 0
  15.     for index in range(size):
  16.         rest_best = max(stock_prices[index+1:], default=-1e9)
  17.         result = max(result, rest_best - stock_prices[index])
  18.     return result
  19.  
  20.  
  21. def get_max_profit3(stock_prices):
  22.     best_rest, size = stock_prices[:], len(stock_prices)
  23.     for index in range(size - 2, -1, -1):
  24.         best_rest[index] = max(best_rest[index], best_rest[index + 1])
  25.     result = 0
  26.     for index in range(size):
  27.         result = max(result, best_rest[index] - stock_prices[index])
  28.     return result
  29.  
  30.  
  31. for _ in range(1000):
  32.     boundry, size = randint(10, 100), randint(10, 100)
  33.     stock_prices = [randint(-boundry, boundry) for _ in range(size)]
  34.     print(stock_prices)
  35.     res1 = get_max_profit1(stock_prices)
  36.     res2 = get_max_profit2(stock_prices)
  37.     res3 = get_max_profit3(stock_prices)
  38.     print(res1, res2, res3)
  39.     assert res1 == res2 == res3, "zonk"
  40.  
Advertisement
Add Comment
Please, Sign In to add comment