编写一个函数,它接受两个自然数和作为输入,returns 总和为的所有大小元组的集合
Write a function that takes two natural numbers and as inputs and returns the set of all tuples of size that sum to
在 python 3 中,我正在尝试编写一个函数,该函数接受两个自然数作为输入,并且 returns 总和为 .
的所有元组的集合
我构建了以下函数:
def get_tuples(length, total):
if length == 1:
yield (total,)
return
for i in range(total + 1):
for t in get_tuples(length - 1, total - i):
yield (i,) + t
其中,例如,list(get_tuples(2, 8)) returns 正确的结果,并且:
def get_tuples(length, total):
if length == 1:
return [(total,)]
comp = []
for i in range(total + 1):
for t in get_tuples(length - 1, total - i):
comp.append((i,) + t)
return comp
然而 returns 一个错误的结果(即单个元组)。
任何人都可以解释为什么以及如何修复第二个功能吗?
给你:
def get_tuples(length, total):
if length == 1:
return [(total,)]
comp = []
for i in range(total + 1):
for t in get_tuples(length - 1, total - i):
comp.append((i,) + t)
return comp
将 return 语句从循环中取出。
只需将 return 缩进一个:
def get_tuples(length, total):
if length == 1:
return [(total,)]
comp = []
for i in range(total + 1):
for t in get_tuples(length - 1, total - i):
comp.append((i,) + t)
return comp
编辑:确保您了解原因
owninggreendragsdude 的回答是正确的,因为它准确地显示了您需要在代码中更改以使其工作的一件事。
但是,如果您想知道如何摆脱递归并同时使代码更快,这里有一个解决方案:
def get_tuples_optimized(length, total):
comp = []
# Stack of partial tuples to process, initialized with an empty tuple
todo = [(total, tuple())]
# Loop as long as we have partial tuples on our stack
while todo:
# Take the next partial tuple
amount_left, partial_tuple = todo.pop()
if len(partial_tuple) == length - 1:
# Put all that is left as the last element
comp.append(partial_tuple + (amount_left,))
else:
# Add all possible extensions-by-one to our stack
for i in range(amount_left + 1):
extended_tuple = partial_tuple + (i,)
todo.append((amount_left - i, extended_tuple))
return comp
在 python 3 中,我正在尝试编写一个函数,该函数接受两个自然数作为输入,并且 returns 总和为 .
的所有元组的集合我构建了以下函数:
def get_tuples(length, total):
if length == 1:
yield (total,)
return
for i in range(total + 1):
for t in get_tuples(length - 1, total - i):
yield (i,) + t
其中,例如,list(get_tuples(2, 8)) returns 正确的结果,并且:
def get_tuples(length, total):
if length == 1:
return [(total,)]
comp = []
for i in range(total + 1):
for t in get_tuples(length - 1, total - i):
comp.append((i,) + t)
return comp
然而 returns 一个错误的结果(即单个元组)。 任何人都可以解释为什么以及如何修复第二个功能吗?
给你:
def get_tuples(length, total):
if length == 1:
return [(total,)]
comp = []
for i in range(total + 1):
for t in get_tuples(length - 1, total - i):
comp.append((i,) + t)
return comp
将 return 语句从循环中取出。
只需将 return 缩进一个:
def get_tuples(length, total):
if length == 1:
return [(total,)]
comp = []
for i in range(total + 1):
for t in get_tuples(length - 1, total - i):
comp.append((i,) + t)
return comp
编辑:确保您了解原因
owninggreendragsdude 的回答是正确的,因为它准确地显示了您需要在代码中更改以使其工作的一件事。
但是,如果您想知道如何摆脱递归并同时使代码更快,这里有一个解决方案:
def get_tuples_optimized(length, total):
comp = []
# Stack of partial tuples to process, initialized with an empty tuple
todo = [(total, tuple())]
# Loop as long as we have partial tuples on our stack
while todo:
# Take the next partial tuple
amount_left, partial_tuple = todo.pop()
if len(partial_tuple) == length - 1:
# Put all that is left as the last element
comp.append(partial_tuple + (amount_left,))
else:
# Add all possible extensions-by-one to our stack
for i in range(amount_left + 1):
extended_tuple = partial_tuple + (i,)
todo.append((amount_left - i, extended_tuple))
return comp