调试硬币改变动态编程

时间:2018-02-18 15:49:37

标签: recursion dynamic-programming

我的硬币更改动态编程实现在某些测试用例中失败了,我很难弄清楚原因:

问题陈述:给定金额和硬币列表,找到赚取金额所需的最小硬币数量。

例如:
目标金额:63
硬币清单:[1,5,10,21,25]
产出:[21,21,21]

#Change file names and folder names in a given directory and all its 
subfolders

import os

os.chdir("path\\to\\folder")
print(os.getcwd())

#Walk the directory and change the file names and folder names in all folders and subfolders.

for root, dirs, files in os.walk("path\\to\\folder"):
    for dir_name in dirs:
        os.rename(dir_name, dir_name.replace(" ", "_").lower())


    for file_name in files:
        os.rename(file_name, file_name.replace(" ", "_").lower())



#Print done once all files have been renamed.      
print("done")

饰品:https://trinket.io/python/43fcff035e

1 个答案:

答案 0 :(得分:2)

您通过在循环期间附加变量来破坏变量changes。试试这个:

替换这两行:

changes.append(change)
coin_count.append(changes)

使用:

_changes = changes[:] + [change]
coin_count.append(_changes)