I wrote a code which on given a sum and a list of coins will return a set of coins that add up to that sum
eg. input: sum = 7 coins = [2,3]
output:
[3,2,2]
but when it was returning none when I debugged the code I found that the result variable was computed correctly but when it appends c it turns into none
def coinSum(sum,coin):
if sum == 0:
return []
if sum < 0:
return None
for c in coin:
reminder = sum - c
result = coinSum(reminder,coin)
if result is not None:
returnValue = result.append(c)
return returnValue
return None
After that, I wrote this code which gave the correct output
def coinSum(sum,coin):
if sum == 0:
return []
if sum < 0:
return None
for c in coin:
reminder = sum - c
result = coinSum(reminder,coin)
if result is not None:
returnValue = result + [c]
return returnValue
return None
what is the difference here? when I check it with a simple list, the append function works properly