Skip to content

Instantly share code, notes, and snippets.

@Sunil-ghodela
Created August 29, 2019 15:59
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Sunil-ghodela/20f27ebcadda07f0c75916c77ad9c665 to your computer and use it in GitHub Desktop.
Save Sunil-ghodela/20f27ebcadda07f0c75916c77ad9c665 to your computer and use it in GitHub Desktop.
Stock Buy Sell to Maximize Profit
minVal = 0
maxVal = 10
stockArray = [100, 180, 260, 310, 40, 535, 695]
def maxProfitAaja(stockList):
start = 0
stockProfitList = []
stockBuy = None
while(start < len(stockList)):
tempStockDiff = 0
if stockBuy == None:
if stockList[start] < stockList[start+1]:
stockBuy = stockList[start]
else:
if start+1 < len(stockList):
if stockList[start] > stockList[start-1]:
# print(start)
# print(stockList[start])
# print(stockList[start+1])
# print(stockList[start] - stockList[start-1])
tempStockDiff = stockList[start] - stockList[start-1]
else:
# print(start)
# print(tempStockDiff)
# print(stockBuy)
stockProfitList.append({ 'stock buy'+ str(stockBuy): 'stock sell' + str(stockList[start-1])})
stockBuy = None
# if tempStockDiffPlusOne > tempStockDiff:
# tempStockDiff = tempStockDiffPlusOne
# else:
# stockProfitList.append({stockBuy:stockList[start]})
start += 1
return stockProfitList
profitList = maxProfitAaja(stockArray)
print(profitList)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment