I am trying to generate a combination of numbers in python list using recursion and my code is as follows
nums = [2,3,4,6]
def backtrck(temp, starting_index, nums):
if len(temp) == 2:
print(temp)
return
temp.append(nums[starting_index])
for i in range(starting_index + 1, len(nums)):
backtrck(temp, i, nums)
backtrck([], 0, nums)
for some reason, the above code is unable to generate the proper combinations.
Aim of the code: I want to generate all the combination of numbers starting with index 0 whose length should be equal to 2
expected output
[2, 3]
[2, 4]
[2, 6]
actual output
[2, 3]
[2, 3]
[2, 3]
[2, 3]
I don't understand what is going wrong with this recursion, I am hoping that someone could help me figure this out