The part [faces_all[0],x]
creates a list of two elements. The first element is faces_all[0]
(in this case always 1) and the second element is x
, which is a tuple of 3 numbers itself. What you want in this case is a list of 4 numbers. You can create it like this: [faces_all[0], x[0], x[1], x[2]]
.
Tip: if you add print(repr(pair))
before the last if-statement you can see the results of the second call to itertools.combinations()
.
Before:
(1, (2, 23, 24))
(1, (3, 22, 24))
(1, (4, 21, 24))
...
After:
(1, 2)
(1, 23)
(1, 24)
(2, 23)
(2, 24)
(23, 24)
(1, 3)
(1, 22)
(1, 24)
(3, 22)
(3, 24)
(22, 24)
(1, 4)
(1, 21)
(1, 24)
(4, 21)
(4, 24)
(21, 24)
...
UPDATE
Now, once you have the correct number pairs, you could check if the sum of the numbers is 25 and break the for-loop if this is the case. (Note that itertools.combinations()
returns a generator, meaning that the next pairs are not 'calculated' if you break from the loop.)
The complete code could be something like this. Note the for-else
statement, which does not exist in most languages, and is convenient in this case. The else statement is reached when the for-loop is completed 'normally' (i.e. without break).
import itertools
faces_all = list(range(1, 25)) # Create list numbered 1-24
for x in itertools.combinations((faces_all), 3): # check combinations in numbers 2-23
if faces_all[0] + sum(x) == 50: # if 1 + combination = 50 continue
four_numbers = (faces_all[0], x[0], x[1], x[2])
for pair in itertools.combinations(four_numbers, 2):
if sum(pair) == 25:
break
else:
print(repr(four_numbers))
The output of above code is:
(1, 4, 22, 23)
(1, 5, 21, 23)
(1, 6, 20, 23)
(1, 6, 21, 22)
(1, 7, 19, 23)
(1, 7, 20, 22)
(1, 8, 18, 23)
(1, 8, 19, 22)
(1, 8, 20, 21)
(1, 9, 17, 23)
(1, 9, 18, 22)
(1, 9, 19, 21)
(1, 10, 16, 23)
(1, 10, 17, 22)
(1, 10, 18, 21)
(1, 10, 19, 20)
(1, 11, 15, 23)
(1, 11, 16, 22)
(1, 11, 17, 21)
(1, 11, 18, 20)
(1, 12, 14, 23)
(1, 12, 15, 22)
(1, 12, 16, 21)
(1, 12, 17, 20)
(1, 12, 18, 19)
(1, 13, 14, 22)
(1, 13, 15, 21)
(1, 13, 16, 20)
(1, 13, 17, 19)
(1, 14, 15, 20)
(1, 14, 16, 19)
(1, 14, 17, 18)
(1, 15, 16, 18)
Is this the result you're looking for?