Python Challenge - Maximize Stock Trading Profit

This community-built FAQ covers the “Maximize Stock Trading Profit” code challenge in Python. You can find that challenge here, or pick any challenge you like from our list.

Top Discussions on the Python challenge Maximize Stock Trading Profit

There are currently no frequently asked questions or top answers associated with this challenge – that’s where you come in! You can contribute to this section by offering your own questions, answers, or clarifications on this challenge. Ask a question or post a solution by clicking reply (reply) below.

If you’ve had an “aha” moment about the concepts, formatting, syntax, or anything else with this challenge, consider sharing those insights! Teaching others and answering their questions is one of the best ways to learn and stay sharp.

Join the Discussion. Help a fellow learner on their journey.

Ask or answer a question about this exercise by clicking reply (reply) below!
You can also find further discussion and get answers to your questions over in #get-help.

Agree with a comment or answer? Like (like) to up-vote the contribution!

Need broader help or resources? Head to #get-help and #community:tips-and-resources. If you are wanting feedback or inspiration for a project, check out #project.

Looking for motivation to keep learning? Join our wider discussions in #community

Learn more about how to use this guide.

Found a bug? Report it online, or post in #community:Codecademy-Bug-Reporting

Have a question about your account or billing? Reach out to our customer support team!

None of the above? Find out where to ask other questions here!

def max_profit_days(stock_prices): max_profit_amt = 0 max_day_1 = 0 max_day_2 = 1 for day in stock_prices: i = stock_prices.index(day) while i < len(stock_prices): if stock_prices[i] - day > max_profit_amt and i != stock_prices.index(day): max_day_1 = stock_prices.index(day) max_day_2 = i max_profit_amt = stock_prices[i] - day i+=1 return max_day_1, max_day_2
def max_profit_days(stock_prices): # Write your code here: max_profit = None for i in range(len(stock_prices)): for j in range(i + 1, len(stock_prices)): profit = stock_prices[j] - stock_prices[i] if max_profit is None or profit > max_profit: max_profit = profit buy_day, sell_day = i, j return (buy_day, sell_day) print(max_profit_days([17, 11, 60, 25, 150, 75, 31, 120]))

def max_profit_days(stock_prices):

Write your code here:

high = -999999
final = [0,0]

for i in range(len(stock_prices)):

for j in range(i + 1,len(stock_prices)):
  if stock_prices[j] - stock_prices[i] >= high:
    high = stock_prices[j] - stock_prices[i]
    final = (i,j)
    print(final)

  else:
    continue

return final

print(max_profit_days([17, 11, 60, 25, 150, 75, 31, 120]))

I made a helper function to find the index of the maximum (only using the part of the list from a specified index forward),
and I used that in a loop to get the max possible profit for buying on any given day,
and then finding the maximum out of all of those.

my code
def index_of_max(arr, starting_index = 0, ending_index = None):
    length = len(arr)
    if length < 1:
      return None
    if ending_index is None:
      ending_index = length - 1
    else:
      length = ending_index + 1
    max_so_far = arr[starting_index]
    max_index = starting_index
    for i in range(starting_index, length):
      if arr[i] > max_so_far:
        max_so_far = arr[i]
        max_index = i
    return max_index


def max_profit_days(stock_prices):

  length = len(stock_prices)
  profits = []
  sell_days = []

  for i in range(length - 1):
    # index i is the day that buy the stock
    differences = [(x - stock_prices[i]) for x in stock_prices]
    best_day = index_of_max(differences, starting_index = i + 1)
    profit = differences[best_day]
    profits.append(profit)
    sell_days.append(best_day)
    #print((i, best_day), profit)
  
  selected_index = index_of_max(profits)
  return (selected_index, sell_days[selected_index])

def max_profit_days(days):
highest_profit=None
b_day=None
s_day=None
days=list(days)
length=len(days)
for buy_day in days:
bday_index=days.index(buy_day)+1
for sell_day_index in range(bday_index,length):
sell_day=days[sell_day_index]
profit=sell_day-buy_day
if highest_profit==None or highest_profit<profit:
highest_profit=profit
b_day=days.index(buy_day)
s_day=days.index(sell_day)
return (b_day,s_day)

print(max_profit_days([17, 11, 60, 25, 150, 75, 31, 120]))