I have an assignment to make a function that receives only an integer and a list of integers(can't add anything else). It should return the number of combinations from the list that sums to k, the order doesn't matter(The coin change problem). Also, I need to use recursion. Here's what I did:
def coin(k, lst):
if k == 0:
return 1
if k < 0:
return 0
else:
sum = 0
for i in range(len(lst)):
sum += coin(k - lst[i], lst)
return sum
The problem is it sums same combination multiple times. For example,
coin(5, [1, 2, 5, 6])
returns 9.
It should return 4 (11111, 122, 1112, 5).