0

So I have a set of 11 variables that I want to assign the numbers 1 to 11 using python. I want to then iterate through all combinations of these assignments and check they complete a series of tests. The 6 tests are such that 6 different combinations of the variables (including a separate 12th variable that retains a constant value (12)) are tested to see if they add to 26. For a permutation to be successful, all combinations must add to 26.

To simplify, say it was just 3 variables assigned the numbers 1-3 I want the program to output a=1,b=2,c=3 then to check this against the criteria before changing the order to a=1,b=3,c=2. Check then change: a=2,b=1,c=3 check and change etc until all combinations have been checked.

I first considered using a list to store the numbers 1 to 11 then just randomising the list order using the shuffle function. I’m not sure how else to iterate through the combinations. The random nature of shuffle would eventually do the job but it would be very slow and certainly not an elegant solution.

Thanks for any help in advance :)

Oliver
  • 11
  • 5
  • `itertools.combinations()` is a built-in solution for this. – jasonharper Jun 04 '20 at 11:34
  • What are the 6 tests? Are you confining your search to only 4 of the 11 variables? Are any of these repeated? – asylumax Jun 04 '20 at 11:34
  • The 6 tests are such that 6 different combinations of the variables (including a separate 12th variable that retains a constant value (12)) are tested to see if they add to 26. For a combination to be successful, all combinations must add to 26. – Oliver Jun 04 '20 at 11:37
  • Does this answer your question? [How to get all possible combinations of a list’s elements?](https://stackoverflow.com/questions/464864/how-to-get-all-possible-combinations-of-a-list-s-elements) – jpf Jun 04 '20 at 11:42
  • It is helpful to modify the question, so it shows this requirement. – asylumax Jun 04 '20 at 11:42
  • Edited. And yes, I believe that post does answer my question, will try some of these options out later. Thanks – Oliver Jun 04 '20 at 11:49

2 Answers2

1

I have since realised after looking through the answers and links provided that what I actually need here is the itertools.permutation function. Since this particular problem requires all permutations of the list of numbers 1-11 and not just combinations of these numbers in a list of a smaller size and the ordering matters.The following link was useful in realising the solution. https://www.tutorialspoint.com/generate-all-permutation-of-a-set-in-python

import itertools
    numbers = list(range(1,12))
    permutations=list(itertools.permutations(numbers,11))

The result of this code provides all possible orders of the numbers 1-11 as a list of tuples which I can then iterate through to check if they pass the tests.

Oliver
  • 11
  • 5
0

The itertools package includes a combinations function that may be helpful for you. For example:

import itertools
numbers = range(1, 12)
for (a,b,c) in itertools.combinations(numbers, 3):
   # do something
a'r
  • 35,921
  • 7
  • 66
  • 67