import itertools
def func(n,l):
return [a for i in [itertools.product(l,repeat=x) for x in range(1,6)] for a in i if sum(a) == n]
print(func(5,[1,2,5]))
from copy import copy
def recurse_find(decomposed,remaining,valid_numbers):
#base case
if remaining == 0:
return decomposed
#find all valid subtractions
else:
ans = []
for number in valid_numbers:
if remaining - number >= 0:
new_decomposed = copy(decomposed)
new_decomposed.append(number)
cand = recurse_find(new_decomposed,remaining-
number,valid_numbers)
if cand:
ans.append(cand)
if len(ans) > 0:
return ans
{1>尝试在CDS之外检查理解力:
输出:
^{pr2}$如果需要名单:
输出:
一个非常自然的公式是:
print(recurse_find([],5,[1,2,5])gt;匹配您请求的输出。在
相关问题 更多 >
编程相关推荐