![]() Min_price = min(min_price, current_price) Max_profit = max(max_profit, potential_profit) Potential_profit = current_price - min_price # min price and sold at the current price # See what our profit would be if we bought at the # max_profit is supposed to be *negative*-we'd return 0.įor current_time in xrange(1, len(stock_prices)):Ĭurrent_price = stock_prices # This would give a profit of 0, which is a problem if our # If we started at index 0, we'd try to buy *and* sell at time 0. # and we can't buy and sell at the same time! ![]() # We can't sell at the first time, since we must buy first, # them to the first price and the first possible profit # We'll greedily update min_price and max_profit, so we initialize Raise ValueError('Getting a profit requires at least 2 prices') How can we adjust our function to return a negative profit if we can only lose money? Initializing max_profit to 0 won’t work. If we’re trying to get rich, we’ll probably care about those numbers. It matters if we would have lost money, and it matters how much we would have lost.
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |