Last active
August 29, 2015 14:18
-
-
Save mikevoets/14185dd01b4d7362f122 to your computer and use it in GitHub Desktop.
Maximum profit of stock prices
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 characters
def get_max_profit_parker(stock_prices_yesterday): | |
# make sure we have at least 2 prices | |
if len(stock_prices_yesterday) < 2: | |
raise IndexError('Getting a profit requires at least 2 prices') | |
# we'll greedily update min_price and max_profit, so we initialize | |
# them to the first price and the first possible profit | |
min_price = stock_prices_yesterday[0] | |
max_profit = stock_prices_yesterday[1] - stock_prices_yesterday[0] | |
for index, current_price in enumerate(stock_prices_yesterday): | |
# skip the first (0th) time | |
# we can't sell at the first time, since we must buy first, | |
# and we can't buy and sell at the same time! | |
# if we took this out, we'd try to buy /and/ sell at time 0. | |
# this would give a profit of 0, which is a problem if our | |
# max_profit is supposed to be /negative/--we'd return 0! | |
if index == 0: | |
continue | |
# see what our profit would be if we bought at the | |
# min price and sold at the current price | |
potential_profit = current_price - min_price | |
# update max_profit if we can do better | |
max_profit = max(max_profit, potential_profit) | |
# update min_price so it's always | |
# the lowest price we've seen so far | |
min_price = min(min_price, current_price) | |
return max_profit | |
def get_max_profit_mike(stock_prices_yesterday): | |
# initialization | |
min_price = stock_prices_yesterday[0] | |
max_price = stock_prices_yesterday[0] | |
max_profit = 0; | |
for current_price in stock_prices_yesterday: | |
# if the current price is smaller than | |
# the min price found | |
if current_price < min_price: | |
# update the minimum price | |
min_price = current_price | |
# update max profit if we can do better | |
max_profit = max(max_profit, max_price - min_price) | |
# if the current price is bigger than | |
# the max price found | |
elif current_price > max_price: | |
# update the maximum price | |
max_price = current_price | |
# reset min price | |
# this because it may happen that the | |
# min price of the day will be before | |
# the max price has come, and since | |
# we must first buy and then sell, a | |
# new min price has to be calculated | |
# from this current price | |
min_price = max_price | |
# update max profit if we can do better | |
max_profit = max(max_profit, max_price - min_price) | |
return max(max_profit, max_price - min_price) | |
day = [100, 50, 0, 25, 35, 45, 70, 90, 150, 200, 250, 230] | |
another_day = [75, 20, 50, 100, 200, 300, 100, 200, 100] | |
a_good_day = [500, 400, 300, 200, 100, 200] | |
a_bad_day = [300, 500, 1000, 1700] | |
print 'results of mike\'s algorithm:' | |
print get_max_profit_mike(day), get_max_profit_mike(another_day), get_max_profit_mike(a_good_day), get_max_profit_mike(a_bad_day) | |
# 100 200 400 0 | |
print 'results of parkers\' algorithm:' | |
print get_max_profit_parker(day), get_max_profit_parker(another_day), get_max_profit_parker(a_good_day), get_max_profit_parker(a_bad_day) | |
# 250 280 100 1400 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment