I have recursive code for generating the Catalan numbers.
I managed to write the recursive call, but for some reason, the counter is not working properly.
For example, the number of calls for the 7th Catalan number should be 1215.
The return value needs to be a tuple of the Catalan number and the number of calls, for example: (429,1215).
Original code:
def catalan_rec(n):
if n<=1:
return 1
res=0
for i in range(n):
res+=catalan_rec(i)*catalan_rec(n-i-1)
return res
Counter code:
def catalan_rec_count(n,counter=1):
if n<=1:
return 1
res=0
for i in range(n):
res+=catalan_rec_count(i,counter+1)*catalan_rec_count(n-i-1,counter+1)
return (res,counter)
res, not a tuple. - Maxcatalan_rec_count(i,counter+1)*catalan_rec_count(n-i-1,counter+1)is multiplying two tuples together. - Max