In python 3 I am trying to write a function that takes two natural numbers and as inputs and returns the set of all tuples of size that sum to .
I have built the following functions:
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
which with, e.g., list(get_tuples(2, 8)) returns the correct results, and:
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
which however returns a wrong result (i.e. a single tuple). Can anybody please explain why, and how to fix the second function?