Is there something I’m missing from the instructions?

I was wondering what I missing in my code. I was only able to get two test to pass in the coding challenge. Is there something I’m missing from the instructions?

def max_profit_days(stock_prices): for i in stock_prices: for j in stock_prices: j = max(stock_prices) i = min(stock_prices) return(stock_prices.index(i),stock_prices.index(j)) print(max_profit_days([17, 11, 60, 25, 150, 75, 31, 140]))

Perhaps try using different variable names on lines 4 and 5 (and use them in line 6).

I went back and labeled them a and b (a being the max of stock_prices and b being the min of stock_prices) and I only pass two tests. I’d be nice to see what the test cases are so that I can work on the code from there.

That’s one possible issue fixed, now to review your logic. What is the goal of this function?

Please post a link to this challenge so we can read the narrative and instructions for better context.

Here’s the link to the challenge:

1 Like

Put in to simplest terms, the expression form is,

return stock_prices.index(min(stock_prices)), stock_prices.index(max(stock_prices))

Were we to write this imperatively without using max() and min() functions, then we would write a loop…

  buy_index = 0
  buy_value = float('inf')  # arbitrarily high value
  sell_index = 0
  sell_value = 0
  for i in range(len(stock_prices)):
    if stock_prices[i] < buy_value:
      buy_value = stock_prices[i]
      buy_index = i
    if stock_prices[i] > sell_value:
      sell_value = stock_prices[i]
      sell_index = i

  return buy_index, sell_index

The above outputs the expected return but only passes two tests, as did the one earlier. Not knowing what the test cases are puts us at a disadvantage. Like you, one is unsure what we’re missing, here.

Something that crossed my mind is that the sell day must be after the buy day. We’ll have to work out the remaining possibilities if the min and max are in reverse. Hmmm…


Going to have someone review the narrative…

you can assume that index 0 represents day 0 and index 7 represents day 7.

Not sure why there are eight elements and not seven.

Thank you for reaching out again! I’ll have to sit down and work through your code. I was also thinking about the sell date being after the buy day. I was thinking about adding a block of code that would look for the next best time to buy and sell so if the max was on day 1 then it would look for the second largest day that would generate a profit.

1 Like