Hello, OnlineGDB Q&A section lets you put your programming query to fellow community users. Asking a solution for whole assignment is strictly not allowed. You may ask for help where you are stuck. Try to add as much information as possible so that fellow users can know about your problem statement easily.

help me understand a python code

+16 votes
asked Dec 16, 2020 by Micho Haddad (220 points)

    cant understand the reccursion here (ans=max(ans,price[i] + solve(length, price, x-length[i], n))    

       def solve(length, price, x, n):
            if x <= 0:  # if x is negative or 0 return 0
               return 0
            ans = 0
            for i in range(0, n):
                if x >= length[i]:
                    ans = max(ans, price[i] + solve(length, price, x - length[i], n))  # recursive solution to find the maximum

            return ans

        plank_length = 4
        length = [1, 2, 3, 4, 5, 6, 7, 8]
        price = [2, 6, 8, 10, 14, 17, 19, 20]
        n = 8

        ans = solve(length, price, plank_length, n)
        print("ans will be: " + str(ans))

2 Answers

+1 vote
answered Feb 1 by Jeff The Chicken (2,860 points)
It calls the function "solve" using the specific variable values in the parenthesis, then prints them.
0 votes
answered Mar 20 by vaishnavi morre (140 points)
'''def solve(length, price, x, n):'''
in this line a new function is formed which can solve
remaining statements are the conditions for the functions.
then the inputs are given .
at the last answer will be printed with the given strings
Welcome to OnlineGDB Q&A, where you can ask questions related to programming and OnlineGDB IDE and and receive answers from other members of the community.