# help me understand a python code

+16 votes
asked Dec 16, 2020

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 (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 (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`