What test cases is my code unable to account for?

I have written a code to solve the Maximize Stock Trading Profit Challenge
Maximize Stock Trading Profit: Maximize Stock Trading Profit | Codecademy
It is able to pass 2 test cases but I want to understand what the other 3 test cases my code is not able to account for.

Below is my code.

``````def max_profit_days(stock_prices):
# Write your code here:
sell_day = 0
buy_day = 0
least_to_greatest = sorted(stock_prices)
greatest_to_least = sorted(stock_prices,reverse=True)
for price in stock_prices:
if price == least_to_greatest[0]:
sell_day += stock_prices.index(price)
elif price == greatest_to_least[0]:
buy_day += stock_prices.index(price)
return (sell_day, buy_day)
``````

Hello @cypherr, welcome to the forums!
If you look through your `for` loop, youâ€™re essentially just setting `sell_day` to `least_to_greatest[0]` and `greatest_to_least[0]`; does that really need the `for` loop? In addition, what if the correct answer isnâ€™t just the smallest number and largest number, consider this array:

``````stock_prices = [100, 1, 2, 3, 10]
``````

Your code returns `(1, 0)`, which means that your code fails any test cases where the answer isnâ€™t the smallest and largest numbers.

One thing you may wish to consider is the fact that after each day, the relevant section of the `stock_prices` array reduces (i.e, after one day, you no longer need to consider the first value. After two days, the first two, etcâ€¦)

I hope this helps, and let us know if you need any more help with this challenge!

1 Like

Oh so then having the loop remove each element from the list until it reaches the least value for the buy_day and vice versa for the sell_day would help?

Perhaps, but again you seem to be focussing on the largest and smallest values for the stock price. Instead, you need to focus on the largest profit, which may not be made with the smallest and largest stock pricesâ€¦

1 Like

Ah! So building a general case to retrieve the biggest difference between two values in the list that occurs first. Makes sense because you canâ€™t just sort real-time occurrences of prices

Exactly! The challenge now is to do it as efficiently as possibleâ€¦Let us know if you want any more help!

So I went back and built a function that passes 4/5 test cases but there is one more that I am not sure it could be. I have even had my code account for an empty list. Oh, it would have to be the case when there is no difference in prices to get a max profit. Essentially the prices are constant right?

``````def max_profit_days(stock_prices):
if len(stock_prices) == 0:
return ()
i = -1
sell_day = 0
buy_day = 0
last_index = stock_prices.index(stock_prices[-1])
max_profit = 0
new_min = min(stock_prices)
new_max = max(stock_prices)
for price in stock_prices:
i += 1
if i == last_index:
break
if stock_prices.index(new_max) == 0:
new_max = max(stock_prices[i+1:])
sell_day = stock_prices.index(new_max)
if stock_prices.index(new_min) >= stock_prices.index(new_max):
new_min = min(stock_prices[:last_index-1])
#new_max = max(stock_prices[i+2:])
else:
sell_day = stock_prices.index(new_max)
buy_day = stock_prices.index(new_min)
max_profit = stock_prices[sell_day] - stock_prices[stock_prices.index(new_min)]
if stock_prices[sell_day] - stock_prices[stock_prices.index(new_min)] > max_profit:
max_profit = stock_prices[sell_day] - stock_prices[stock_prices.index(new_min)]
buy_day = stock_prices.index(min(stock_prices[i+1:]))
else:
continue
return (buy_day, sell_day)

print(max_profit_days([100, 1, 2, 3, 10]))
```[quote="codeneutrino, post:6, topic:718419, full:true"]
Exactly! The challenge now is to do it as efficiently as possible...Let us know if you want any more help! :smiley:
[/quote]``````

There are a number of issues with the code. Have a look at some examples in which your code returns a different answer than expected:

``````print(max_profit_days([150, 11, 60, 25, 150, 75, 31, 120]))
# Your output: (1, 7)
# Expected output: (1, 4)

print(max_profit_days([17, 11, 60, 25, 150, 75, 31, 17]))
# Your output: (0, 0)
# Expected output: (1, 4)

print(max_profit_days([160, 11, 60, 25, 150, 75, 31, 160]))
# Your output: (0, 0)
# Expected output: (1, 7)

print(max_profit_days([150, 11, 150, 11, 150, 75, 11, 90]))
# Your output: (1, 7)
# Expected output: (1, 2)

print(max_profit_days([17, 11, 60, 25, 150, 5, 31, 120]))
# Your output: (5, 0)
# Expected output: (1, 4)
``````

Also, the sell date canâ€™t be the same as the buy date (sell date canâ€™t be less or equal to the buy date). The prices are listed at the opening of each day. So, if you have bought a stock on the first day, you must wait at least till the next day to sell the stock.

``````print(max_profit_days([21, 21, 21, 21, 21, 21, 21, 21]))
# Your output: (0, 0)
# Expected output: (0, 1)

print(max_profit_days([87, 86, 80, 75, 60, 44, 42, 11]))
# Your output: (7, 1)
# Expected output: (0, 1)
# Even though it is a loss, it is the best we can do.
``````