我很难理解下面的代码。 它使用可用面额的硬币('硬币')计算赚钱数量的方式(' n')
def change(n, coins_available, coins_so_far):
if sum(coins_so_far) == n:
yield coins_so_far
elif sum(coins_so_far) > n:
pass
elif coins_available == []:
pass
else:
for c in change(n, coins_available[:], coins_so_far+[coins_available[0]]):
yield c
for c in change(n, coins_available[1:], coins_so_far):
yield c
n = 15
coins = [1, 5, 10, 25]
solutions = [s for s in change(n, coins, [])]
for s in solutions:
print s
输出:
[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,5] 我相信' coins_so_far'当' coins_available'时,仍将在第二次迭代中保持[1,1,1,1,1,1,1,1,1,1,1,1,1,1,1]。持有[5,10,25]
非常感谢任何帮助。感谢
答案 0 :(得分: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.)
答案 1 :(得分:1)
由于您自己在跟踪程序时遇到问题,因此请在进入和退出时添加打印语句。在每种情况下,打印有用的变量。你可以自己关注这个吗?更好的是,你可以将这种做法应用到你自己的编码中吗?
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
答案 2 :(得分:0)
我不明白[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),取代五个硬币。