I want to calculate the number of subsets of the array A = [2, 1, 1, 4] that sum to 2. There are 2 ways: (A[0]) and (A[1], A[2]). My code for computing this is:
def W(number, index):
A = [2, 1, 1, 4]
if number < 0 or index < 0:
return 0
elif number==0:
return 1
else:
return W(number, index-1) + W(number - A[index], index)
Right now, when I call the function with W(2,3), I get 4 instead of 2. My problem is that my code also calculates the possibility (A[1], A[1]) and (A[2], A[2]). Is there any way to fix it while still using recursion?