Find all possible subsets that sum up to a given number

前端 未结 4 681
既然无缘
既然无缘 2021-01-21 20:32

I\'m learning Python and I have a problem with this seems to be simple task.

I want to find all possible combination of numbers that sum up to a given number.
for

4条回答
  •  日久生厌
    2021-01-21 20:59

    This is equivalent to the problem described in this question and can use a similar solution.

    To elaborate:

    def allSum(number):
        for solution in possibilites(range(1, number+1), number):
            expanded = []
            for value, qty in zip(range(1, number+1), solution):
                expanded.extend([value]*qty)
            yield expanded
    

    That translates this question into that question and back again.

提交回复
热议问题