Getting Runtime error for this code :-
import numpy as np
def combination(inp,n,k,ans):
if len(ans)==k:
print(*ans)
else:
if len(inp)>0:
b=[]
b=ans.append(inp[0])
combination(inp[1:],n,k,b)
combination(inp[1:],n,k,ans)
n=int(input())
a=list(map(int,input().split()))
a=np.array(a)
k=int(input())
ans=[]
combination(a,n,k,ans)
But why it is showing run Time error for this ?
Recursion is a functional heritage and so using it with functional style yields the best results. This means avoiding things like mutations, variable reassignments, and other side effects.
We can write fixed-length combinations
choosekusing inductive reasoning -kis zero, yield the empty combinationkis greater than zero. if the iterableitis empty, stop iterationkis greater than zero and the iterableithas at least one element. for eachcombof the sub-problemchoosek(it[1:], k - 1), prepend the first element ofitto the resultingcomband yield each result of the sub-problemchoosek(it[1:], k)Notice the
printside effect is moved outside of the function so the caller can do whatever they choose with the resulting combinations -