fork(2) download
  1. def recursion_change(coins, money):
  2. """
  3. Returns a tuple containing:
  4. :an array counting which coins are used to make change, mirroring the input array
  5. :the number of coins to make tender.
  6.  
  7. :coins: List[int]
  8. :money: int
  9. :rtype: (List[int], int)
  10. """
  11. change_list = [0] * len(coins)
  12.  
  13.  
  14.  
  15. def _helper_recursion_change(i, k, change_list):
  16. if k == 0: # Base case: money in this (sub)problem matches change precisely
  17. return 0
  18. elif i == -1 or k < 0: # Base case: change cannot be made for this subproblem
  19. return float('inf')
  20. else: # Otherwise, simplify by recursing:
  21. # Take the minimum of:
  22. # the number of coins to make i cents
  23. # the number of coins to make k-i cents
  24. return min(_helper_recursion_change(i-1, k, change_list), 1 + _helper_recursion_change(i, k-coins[i], change_list))
  25. return (_helper_recursion_change(len(coins)-1, money, change_list))
  26.  
  27. print str(recursion_change([1, 5, 10, 25, 50, 100], 6)) # Current Output: 2
  28.  
  29. # Desired Output: ([1, 1, 0, 0, 0, 0], 2)
Success #stdin #stdout 0.02s 9016KB
stdin
Standard input is empty
stdout
2