Last active
September 4, 2020 18:18
-
-
Save willy-r/8a052d6684c6819cff5c51c91e054305 to your computer and use it in GitHub Desktop.
Given an array of numbers that represent stock prices (where each number is the price for a certain day), find 2 days when you should buy and sell your stock for the highest profit.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def stock_buy_sell(prices: list): | |
"""Returns the 2 days when you should buy and sell the stock for the | |
highest profit. | |
""" | |
buy_day = prices.index(min(prices)) + 1 | |
sell_day = prices.index(max(prices[buy_day:])) + 1 | |
return f'Buy on day {buy_day}, sell on day {sell_day}' | |
if __name__ == '__main__': | |
prices = [110, 180, 260, 40, 310, 535, 695] | |
print(stock_buy_sell(prices)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment