It's a situation that's caused by a lack of understanding of recursive functions, but it's not resolved <
I want to add the nCr result to the list using the recursive function
Simply put, if it is 4c2, the result value 1, 2/1, 3/1, 4/2, 3/2, 4/3, 4 is added to the specific list
If you print (result) in the r==0 situation, it will be printed normally
If you use fianl.append(result) and put it into the final, the final will only contain the final value of the recursive function. I've tried dealing with global variables and this and that, but it's not solved. Can you tell me why?
In the r==0 situation, once the result comes out, you have to put it in the final, but it seems that you put the last result value in the final after the recursive function Why is that?
I think n=int('n:') doesn't work because it's running on the web page;
n=int(input('n:'))
r=n//2
# Just briefly find the nC(n/2) situation
li=[x+1 for x in range(n)] Create a list from #1 to n
result=[0]*r #Each result value
final=[] # The final list that you want to include the results
def comb(n,r):
if r==0:
final.append(result)
return
elif n<r:
return
else:
result[r-1]=li[n-1]
comb(n-1,r-1)
comb(n-1,r)
comb(n,r)
print(final) #It is not the normal output expected here, but only enters the final value when the recursive function is rotated.
final.append(result[:])
If you add the reference result, they all have the same result value. Copy the copy
and save the result at the time of append.
© 2024 OneMinuteCode. All rights reserved.