简体   繁体   English

我真的很需要这个python代码解释

[英]I really need this python code explanation

I have a code in python that uses amount and coins change array as parameters and based on a possible number of combinations of the coins to be used to provide the money or amount to be given as complete indepth.我在 python 中有一个代码,它使用金额和硬币变化数组作为参数,并基于硬币的可能组合数量来提供金钱或金额作为完整的深度。 but the issue here is that I seem not to have a clue how this recursion works但这里的问题是我似乎不知道这个递归是如何工作的

I already found the working code but I need some explanation about how thos works since I got this with my code called countChange(4, [1,2]) .我已经找到了工作代码,但我需要一些关于这些工作原理的解释,因为我使用名为countChange(4, [1,2])的代码得到了这个。 The issue here is how does it work because I noticed with the code below, the amount started reducing and the the array was zero这里的问题是它是如何工作的,因为我注意到下面的代码,数量开始减少并且数组为零

def countChange(amount, coins):
    if (amount == 0):
        print("amount is 0", amount, coins)
        return 1
    elif (amount < 0 or coins == []):

        print("amount is less than 0 or empty coins array", amount, coins)
        return 0
    else:
        countChange(amount, coins[:-1])
        print("first called")
        countChange(amount - coins[-1], coins)
        print("second called")

countChange(4, [1,2])

I really dont undertand how come this correct result happens.我真的不明白这个正确的结果是怎么发生的。

from python3 myprogram_name.py
amount is less than 0 or empty coins array 4 []
first called
amount is less than 0 or empty coins array 3 []
first called
amount is less than 0 or empty coins array 2 []
first called
amount is less than 0 or empty coins array 1 []
first called
amount is 0 0 [1]
second called
second called
second called
second called
first called
amount is less than 0 or empty coins array 2 []
first called
amount is less than 0 or empty coins array 1 []
first called
amount is 0 0 [1]
second called
second called
first called
amount is 0 0 [1, 2]
second called
second called

as it goes back and forth当它来回移动时

At first time,第一次,

countChange(4, [1,2])

the amount is not zero, and the coins array was not empty, else statement gets worked.金额不为零,并且硬币数组不为空,否则语句将起作用。 Inside the else statement, there is a recursion call with,在 else 语句中,有一个递归调用,

countChange(4, [1])-------->opened

then again comes into else statement, and again recursion called,然后再次进入 else 语句,再次调用递归,

countChange(4, [])----->opened

Now comes to the elif statement, prints the string and return zero called.so the final call of recursion going to be ended.现在来到 elif 语句,打印字符串并返回零调用。所以递归的最终调用将结束。

countChange(4, [])------>ended..

still we have remains three statement on that else block.我们仍然在那个 else 块上有三个声明。

print("first called")
countChange(amount - coins[-1], coins)
print("second called")

So according to that, the "first called" string gets printed.And again we have a recursive call, Now we subtract the amount value by counts[-1], so amount = 4 - 1 = 3.因此,根据这一点,“第一次调用”字符串被打印出来。我们再次进行递归调用,现在我们将数量值减去 counts[-1],所以数量 = 4 - 1 = 3。

countChange(3, [1])----->opened

Similar to again, else statement gets activated, and again having a recursive call,与再次类似,else 语句被激活,并再次进行递归调用,

countChange(3, [])----->opened

This way, if you trace recursion, you can get cleared., Hope this helps you:)这样,如果你跟踪递归,你就可以被清除。希望这对你有帮助:)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM