You are trying to use backtracking to calculate the subsets that add up to a target. However, in your code, you keep removing the item from the arr, and it was not added back for backtracking. That's why it prints only 4 times, as the arr size is getting smaller during the recursion.
But in this case, your arr should not be changed, as it can be in different subsets to make up to the target, for example the number 10
in your example. So I would use an index to note the current position ,and then exam the remaining to see if it adds up to a target.
Here is my sample code for your reference. I used an int array as a mutable result holder to hold the result for each recursion.
def num_sum(current_index, target, arr, ans):
if target == 0:
ans[0]+=1;
return
elif target < 0:
return
for i in range(current_index, len(arr)):
num_sum(i+1, target - arr[i], arr, ans)
ans = [0]
num_sum(0, 16, [2, 4, 6, 10, 8], ans)
print(ans[0])
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…