-2

How can we change this recursive function to "for iteration"?.....
Note: Greed algorithms should not be used. Greedy algorithms are less accurate. Reason to change : To increase the efficiency of a function by changing to a dynamic algorithm What I've tried:

# this code is trash 
for i in range(1,m+1):
    n1=m-coins[i]
for i in range(1,m+1):
    n2=m-coins[i]
return min(n1,n2) 

while n1==0:
    i=i+1
    n1=m-coins[i]
return n1        

Besides this, I tried using dictionary and combination, but I forgot because I thought it wouldn't work.
How the function behaves : It is a function that takes a list of coins and a value of m and makes m==0.
n1 means moving on without using coins[0].
n2 means using coins[0] as a necessity and moving on to the next level.
If I had a list of [5,4,2,1] and a value of 10 m, I would recurs the first n1=[4,2,1],10 and the first n2=[5,4,2,1],10.

    coins =[50,40,20,10,5,4,2,1]  
    m = 80

    def coin_count(coins, m):
        if m > 0:
            while len(coins) > 0 and m < coins[0]:
                coins = coins[1:] 
#Below is the part I want to change.
            if coins[0] > 1:
                n1=coin_count(coins[1:],m)
                n2=coin_count(coins,m-coins[0])+1
                return min(n1,n2)
            else: # coins[0] == 1
                return m
#Above is the part I want to change
        else: # m == 0
            return 0
Heo
  • 11
  • 1
  • What is the function doing? Why do you want to change it to iteration? What have you tried so far? – oskros May 20 '21 at 06:41
  • I modified code – Heo May 20 '21 at 06:55
  • sorry I dont understand your explanation of the function. You have a list of coins and a value of m..? Are you trying to find the combination of coins that would be equal to `m` when summing them up? – oskros May 20 '21 at 07:04
  • Yes, I want to use part of the list to match the m value. It's kind of a change calculation. – Heo May 20 '21 at 07:16

1 Answers1

0

Here is my solution to your function using a while loop. It returns the smallest possible list of coins that sums up to m - I suppose this is what you were looking for?

How it works is that in each while loop iteration, it takes the current largest coin and checks if the remainder of m is divisible by that coin, adding the number of coins needed and setting m to the remainder

def coin_count(coins, m):
    coins = sorted(coins, reverse=True)
    out = []
    while m > 0:
        cur_coin = coins.pop(0)
        out.extend([cur_coin] * (m // cur_coin))
        m %= cur_coin
    return out

NB: If the input coins are always sorted in descending order, you can remove the first line.

Example:

coins = [50, 40, 20, 10, 5, 4, 2, 1]
m = 128

print(coin_count(coins, m))

[50, 50, 20, 5, 2, 1]

oskros
  • 3,101
  • 2
  • 9
  • 28
  • Is there any way not to use the greedy algorithm? I used n1,n2 on purpose not to use greedy algorithms. – Heo May 20 '21 at 07:23
  • if coins[50,40,20,10,5,4,2,1] and m=80; function coin_count will return [50,40,10] but i want receive [40,40] – Heo May 20 '21 at 07:23
  • You can just look on wikipedia if you want dynamic programming solution: https://en.wikipedia.org/wiki/Change-making_problem#Simple_dynamic_programming – oskros May 20 '21 at 07:26
  • 1
    I'm trying to understand after reading what you sent me. As I read this, I found myself weak in repetition. I'll study more repetitive sentences when this problem is over. Thank you for your kindness! – Heo May 20 '21 at 07:41
  • If I understand Wikipedia, I think I can solve my problem – Heo May 20 '21 at 07:42
  • you can try this solution https://stackoverflow.com/questions/64409483/minimum-coin-change-problem-backtracking – oskros May 20 '21 at 08:04