Search code examples
pythonrecursionlimitcombinations

How to limit Python recursive combinations?


I am currently trying to solve a how to get this piece of code to iterate through the different combinations of numbers below to equal 1200. Which works fine however I want the code to limit the numbers it explores and print the combinations with only 5 different numbers.

E.g1 70, 260, 280, 290, 300 = 1200, uses 5 numbers. I want only these combinations.

E.g2 10, 20, 30, 40, 110, 120, 160, 190, 240, 280 = 1200, uses 10 numbers. I don't want combinations with less than five or greater than 5, like this combination.

I don't know python too well, I feel like its a simple thing to do but with my limited coding knowledge I'm stuck.

#!/usr/local/bin/python
from itertools import combinations

def find_sum_in_list(numbers, target):
    results = []
    for x in range(len(numbers)):
        results.extend(
            [   
                combo for combo in combinations(numbers ,x)  
                    if sum(combo) == target    
            ]
    )
    print results

if __name__ == "__main__":
    find_sum_in_list([10,20,30,40,50,60,70,80,90,100,110,120,130,140,150,160,170,180,190,200,210,220,230,240,250,260,270,280,290,300], 1200)

Thanks for any help. Much appreciated.


Solution

  • You are on the right track but I don't think you need to do something recursive. I think this works.

    from itertools import combinations
    
    def find_sum_in_list(numbers, target, comboSize):
        results = []
        for combo in combinations(numbers, comboSize):
            if sum(combo) == target:
                results.append(combo)
        return results
    
    
    if __name__ == "__main__":
        numbers = [10,20,30,40,50,60,70,80,90,100,110,120,130,140,150,160,170,180,190,200,210,220,230,240,250,260,270,280,290,300]
        total = 1200
        comboSize = 5
        print find_sum_in_list(numbers, total, comboSize)