Stock Buy and Sell to earn Maximize Profit programme in Python

Stock Buy and Sell to earn Maximize Profit programme in Python

def sell_profit(price,n):    

    i = 0
    while(i<n-1):
       """We check if next price is less so we use next price becaue we need minimum
          buy amount."""
        while(i<(n-1and price[i+1] <= price[i]):
            i += 1

        if i == n-1:
            break

        buy = i 
        i += 1
        
        """we check if previous price is less than our current price so we 
            increment price and again check it previous price 
            because we need maximum profit for selling."""

        while(i<(n) and price[i] >= price[i-1]):
            i += 1

        sell = i-1

        print("buy on the day : {} , sell on the day : {}" . format(buy,sell))

price = [50,100,250,300,30,540,620]
n = len(price)
sell_profit(price,n)


# Time Complexity O(n)

# For more code please visit : https://github.com/UjjwalSharma1244/Python

# Ujjwal Sharma

Comments

Popular posts from this blog

Questions

Text Mining In Python For Beginners to Advance

Full Forms Of Computer, www, pdf, Http, ok, BSNL, JIO, AIRTEl, SIM, VoLTE, GOOGLE and many more

Docker Tutorial For Beginners: Basic Commands of Docker