Jamal
Jamal

Reputation: 105

Given a list of numbers find all the permutations that add to 100 of a certain length

Given a list of numbers, list = [0,1,2,10,21,25,30,33,34,35], I want to find all the permutations of those numbers that sum to 100 on python.

However I would like to be able to use the values in the list more than once and I want to be able to select the length of the permutation.

For example if the length of the permutation was 3, I would return: [[30, 35, 35], [33, 33, 34], [33, 34, 33], [34, 33, 33], [35, 30, 35], [35, 35, 30]].

I have already been able to do this for length 3. However to do so I found all the combinations of 3 numbers that sum to 100 and then filtered through that to remove solutions that contain numbers outside of my original list.

The issue is that I cannot follow this approach for permutations of length 4,5,6 as there are a lot more combinations.

Thanks

Upvotes: 0

Views: 619

Answers (1)

anyryg
anyryg

Reputation: 313

I'm not sure if I understood your problem correctly, but try this:

from itertools import product

def special_permutation(lst, rno, suma):
  results = []
  for c in product(lst, repeat = rno):
      if sum(c) == suma:
        results.append(c)
  return results

Upvotes: 2

Related Questions