python递归循环之刻录歌曲

找到最好的解决方法使剩的空间最少,但是我感觉递归的没问题,就是结果不是很对# recursive backtracking algorithm to solve CD problem

inputs: 1. a list of songs given as (title,length) tuples

2. a (maybe partly-filled) CD, represented by an integer measuring remaining space

output: 1. list of song titles accommodated on CD, minimising the amount of remaining space

2. integer representing remaining space

someSongs = [("a large song", 60), ("a little song", 10), ("a bigger song", 20), ("a very short song", 2),
("a tiny song", 1)]

def CDSolve(songsToUse, space):
# returns a list of accommodated songs as (title,length tuples) and remaining space
list = []
# # base cases
if len(songsToUse) == 0:
return list, space
# # if there are no more songs to consider:
# # return solution
if space == 0:
return list, space
# # if there is no more room on the CD:
# # return solution



不知道你这个问题是否已经解决, 如果还没有解决的话:

如果你已经解决了该问题, 非常希望你能够分享一下解决方案, 以帮助更多的人 ^-^