Enable Javascript in your browser and then refresh this page, for a much enhanced experience.
2 pythons enter, 1 python leaves. Which way should i focus on learning? solution in Clear category for Stock Profit by Bifftastic
def stock_profit(stock: list[int]) -> int:
buy = min(stock[:-1])
market = stock.index(buy)
sell = max(stock[market:])
profit = sell - buy
return profit if profit > 0 else 0
# I couldn't figure out a way to make this work as a one liner.
# put a one liner that i got from chatgpt out. couldnt find another site to help explain what i wanted to do.
# I'm submitting my code. Its all me and timeit says mine is waaaay faster than the one liner
# If your reading this, Which is the best way to solve this problem?
#chatgpt
#return max([sell - buy for i, buy in enumerate(stock[:-1]) for sell in stock[i + 1:] if sell > buy], default=0)
print("Example:")
print(stock_profit([3, 1, 3, 4, 5, 1]))
# These "asserts" are used for self-checking
assert stock_profit([2, 3, 4, 5]) == 3
assert stock_profit([3, 1, 3, 4, 5, 1]) == 4
assert stock_profit([4, 3, 2, 1]) == 0
assert stock_profit([6, 2, 1, 2, 3, 2, 3, 4, 5, 4]) == 4
assert stock_profit([1, 1, 1, 2, 1, 1, 1]) == 1
assert stock_profit([4, 3, 2, 1, 2, 1, 2, 1]) == 1
assert stock_profit([1, 1, 1, 1]) == 0
print("You are the best broker here! Click 'Check' to earn cool rewards!")
Sept. 22, 2023
Comments: