Python 无法理解代码段的流程

Python 无法理解代码段的流程,python,recursion,dynamic-programming,np,Python,Recursion,Dynamic Programming,Np,我很难理解下面的代码。 它计算使用可用面额的硬币(“硬币”)制造货币量(“n”)的方法的数量 输出: [1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1] [1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 5] [1, 1, 1, 1, 1, 5, 5] [1, 1, 1, 1, 1, 10] [5, 5, 5] [5, 10] 我知道第一次迭代是如何产生[1,1,1,1,1,1,1,1,1,1,1,1,1,1,1]的,我不知道[1,1,1,1

我很难理解下面的代码。 它计算使用可用面额的硬币(“硬币”)制造货币量(“n”)的方法的数量

输出:

[1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1]
[1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 5]
[1, 1, 1, 1, 1, 5, 5]
[1, 1, 1, 1, 1, 10]
[5, 5, 5]
[5, 10]
我知道第一次迭代是如何产生[1,1,1,1,1,1,1,1,1,1,1,1,1,1,1]的,我不知道[1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1]是如何转换成[1,1,1,1,1,1,1,1,1,1,1,5] 我相信在第二次迭代中,当“硬币可用”保持[5,10,25]时,“硬币到目前为止”仍将保持[1,1,1,1,1,1,1,1,1,1,1]

非常感谢您的帮助。谢谢

我不明白[1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1]是如何转换成[1,1,1,1,1,1,1,1,1,1,5]的

从任何意义上讲,它都没有被“转化”。
change
函数返回一个列表,您的print语句使用列表理解来打印多个结果

不同之处很明显,第一组硬币都是便士(1),而第二组硬币则是一枚镍币(5),用五个便士代替。

英语:

How to determine the ways to make change(for n cents, using coins in
coins_available denominations, and which include a pile of coins_so_far):
    If the coins_so_far sum to n:
        That is the unique way to do it.
        (Because we've already added enough coins to the pile to make
        the desired amount of change.)
    Otherwise, if the coins_so_far sum to more than n:
        There are no ways to do it.
        (Because there's already too much in our pile; we can't fix
        this by adding more.)
    Otherwise, if there are no coins_available:
        There are no ways to do it.
    Otherwise:
        (To make change for the rest of the pile, we either *do* add
        at least one more of the smallest possible coin, or we *don't*.)
        Every way to make change(for n cents, using the same
        coins_available, and which includes the coins_so_far
        as well as the first of the coins_available):
            Is one of the ways to do it.
            (Try adding a coin of the smallest denomination to the pile,
            and then figure out all the ways to make up the rest of the
            change. This finds the ways that do add the small coin.)
        Every way to make change(for n cents, using all the
        coins_available except the first, and including the
        coins_so_far):
            Is also a way to do it.
            (Find the ways that don't add the smallest coin denomination,
            by excluding it from the denominations we'll consider, and
            using the same process.)

由于您自己跟踪程序时遇到问题,请在入口和出口处添加打印语句。在每种情况下,打印有用的变量。你能自己跟上吗?更好的是,您能将这种做法应用到您自己的编码中吗

def change(n, coins_available, coins_so_far):
    print "ENTER: n=", n, "\tavailable=", coins_available, "\t so far:", coins_so_far
    if sum(coins_so_far) == n:
        print "EXIT1: good solution", coins_so_far
        yield coins_so_far
    elif sum(coins_so_far) > n:
        print "EXIT2:", coins_so_far, "exceeded", n, ".  Back up."
        pass
    elif coins_available == []:
        print "EXIT3: out of coins"
        pass
    else:
        for c in change(n, coins_available[:], coins_so_far+[coins_available[0]]):
            print "EXIT4:", c
            yield c
        for c in change(n, coins_available[1:], coins_so_far):
            print "EXIT5:", c
            yield c

n = 15
coins = [1, 5, 10, 25]

solutions = [s for s in change(n, coins, [])]
for s in solutions:
    print s

[s代表foo中的s]
foo
相同。列表理解只是将列表元素逐个复制到一个相同的新列表中。我建议编辑您的问题以删除该列表。@dsh
change(n,coins,[])
返回一个生成器,而不是列表。但是,该生成器仍然可以直接迭代,而不会复制到列表中,是的。虽然这很有用,但它也让我很高兴我们用代码而不是简单的英语编写程序。如果我不得不在课堂上演示代码,我或多或少会讲述代码。:)谢谢@Karl。。这个解释很有帮助:)谢谢,我明白了。
def change(n, coins_available, coins_so_far):
    print "ENTER: n=", n, "\tavailable=", coins_available, "\t so far:", coins_so_far
    if sum(coins_so_far) == n:
        print "EXIT1: good solution", coins_so_far
        yield coins_so_far
    elif sum(coins_so_far) > n:
        print "EXIT2:", coins_so_far, "exceeded", n, ".  Back up."
        pass
    elif coins_available == []:
        print "EXIT3: out of coins"
        pass
    else:
        for c in change(n, coins_available[:], coins_so_far+[coins_available[0]]):
            print "EXIT4:", c
            yield c
        for c in change(n, coins_available[1:], coins_so_far):
            print "EXIT5:", c
            yield c

n = 15
coins = [1, 5, 10, 25]

solutions = [s for s in change(n, coins, [])]
for s in solutions:
    print s