Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- from itertools import combinations
- from random import randint
- def get_max_profit1(stock_prices):
- size, result = len(stock_prices), 0
- for index1, index2 in combinations(range(size), r=2):
- difference = stock_prices[index2] - stock_prices[index1]
- result = max(result, difference)
- return result
- def get_max_profit2(stock_prices):
- size, result = len(stock_prices), 0
- for index in range(size):
- rest_best = max(stock_prices[index+1:], default=-1e9)
- result = max(result, rest_best - stock_prices[index])
- return result
- def get_max_profit3(stock_prices):
- best_rest, size = stock_prices[:], len(stock_prices)
- for index in range(size - 2, -1, -1):
- best_rest[index] = max(best_rest[index], best_rest[index + 1])
- result = 0
- for index in range(size):
- result = max(result, best_rest[index] - stock_prices[index])
- return result
- for _ in range(1000):
- boundry, size = randint(10, 100), randint(10, 100)
- stock_prices = [randint(-boundry, boundry) for _ in range(size)]
- print(stock_prices)
- res1 = get_max_profit1(stock_prices)
- res2 = get_max_profit2(stock_prices)
- res3 = get_max_profit3(stock_prices)
- print(res1, res2, res3)
- assert res1 == res2 == res3, "zonk"
Advertisement
Add Comment
Please, Sign In to add comment