Oct-10-2019, 07:05 AM
New to Python and Algorithims. Trying to solve this one before looking for solutions. Im having the problem towards the bottom where the coin is suppose to be appended to the list, if this doesn't happen then the condition never becomes false.
def rec_coins(target,coins): coins = sorted(coins) mincoinsneeded = [] sum_of_coins = sum(mincoinsneeded) current_accum = target - sum_of_coins while sum_of_coins != target: for coin in reversed(coins): if coin == target: return coin elif current_accum % coin != 0: coins.pop() else: while current_accum % coin > 1: mincoinsneeded.append(coin) print (mincoinsneeded) rec_coins(20,[1,5,10])