Python_002: Everyone loves wine

Everyone loves wine


Hey friends, 
Yesterday I found one interesting problem on Codechef. I really liked that, so I started solving it. After some mistakes finally I found. Please try to solve it. The programe is based on recursion. 

Chef has brought N wines from French to sell in his restaurant. Chef sells one wine per day and wants to maximize the money he receives over a given period time. These wines are interesting for many reasons:
  • The wines are numbered 1..N and stored sequentially in a single file in a long box which is opened at both ends. On any day, the chef can retrieve one wine from either end of his stash of wines.
  • The taste of wines improve with age and so he can sell it for the greater price. The wines are not uniform: some are better and made with finner ingredients. wine i has cost c(i).
  • The chef can sell the wine that has aged longer for the greater price: he can sell it for a price c(i)*a for a treat of age a.
  • Given the costs c(i) of each of the wine lined up in order of the index i in their box, what is the maximum money chef can get for them if he orders their sale optimally?
    The first wine is sold on day 1 and has age a=1. Each subsequent day increases the age by 1.

    Input

    • The first line contains a single integer N denoting the number of wines in the stash
    • From line 2 to line N+1: Line i+1 contains a single integer denoting the cost of wine c(i).

    Output

    • The output is a single line containing the maximum money the chef can get if he sells them optimally.

    Example

    Input1:
    4
    10
    1
    10
    9
    Output:
    79
    CodeChef Link : click here
    Try to solve it. 
    Here is my solution in python

    #Function for finding maximum profit
    def maxpro(plvar):
        l=len(plvar)
        d = N - len(plvar) + 1 #Count Day no
        fprice=plvar[0] #first price in list
        lprice = plvar[len(plvar)-1] #last price in list
        if(len(plvar)>1):
            f_rem_list=plvar[1:len(plvar)] #remaining list after removing first price
            l_rem_list=plvar[0:len(plvar)-1] #remaining list after removing last price
            return max(fprice*d + maxpro(f_rem_list),lprice*d + maxpro(l_rem_list)) 
        else:
            return fprice*d #for last day-only one bottle will remain
        
    
    
    #Function for finding minimum profit
    def minpro(plvar):
        l=len(plvar)
        d = N - len(plvar) + 1   
        fprice=plvar[0]
        lprice = plvar[len(plvar)-1]
        if(len(plvar)>1):
            f_rem_list=plvar[1:len(plvar)]
            l_rem_list=plvar[0:len(plvar)-1]
            return min(fprice*d + minpro(f_rem_list),lprice*d + minpro(l_rem_list))
        else:
            return fprice*d
    
    
    N = 8 #No. of bottle of wine
    pl=[10,22,77,4,55,1,10,9]  #price list according to wine
    print ("Wine : ",pl)
    print("Maximum profit : ",maxpro(pl))
    print("Minimum profit : ",minpro(pl))
    
    
    
    
    Click here to download source code
    
    
    
    

Comments