Set of enumerate items in list in Python [closed] - python

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 3 years ago.
Improve this question
I need to create a lot of sets of numbers. I add my output and the output that I need.
I wrote the first item output of my list, then I need the same for all items.
a = [1, 2, 3, 4, 5, 6, 7, 8]
for index, item in enumerate(a):
index_0 = index
index_1 = index + 1
index_2 = index + 2
index_3 = index + 3
print((a[index_0], a[index_1], a[index_2], a[index_3]))
>>>
1, 2, 3, 4
2, 3, 4, 5
3, 4, 5, 6
4, 5, 6, 7
5, 6, 7, 8
What I'm looking for for each item:
1,2,3,4
1,2,3,5
1,2,3,6
1,2,3,7
1,2,3,8
1,2,4,5
1,2,4,6
1,2,4,7
1,2,4,8
1,2,5,6
1,2,5,7
1,2,5,8
1,2,6,7
1,2,6,8
1,2,7,8
1,3,4,5
1,3,4,6
1,3,4,7
1,3,4,8
1,3,5,6
1,3,5,7
1,3,5,8
1,3,6,7
1,3,6,8
1,3,7,8
1,4,5,6
1,4,5,7
1,4,5,8
1,4,6,7
1,4,6,8
1,4,7,8
1,5,6,7
1,5,7,8
1,6,7,8

Are you looking to use itertools combinations:
from itertools import combinations
list(combinations([1,2,3,4,5,6,7,8], 4))
Output:
[(1, 2, 3, 4),
(1, 2, 3, 5),
(1, 2, 3, 6),
(1, 2, 3, 7),
(1, 2, 3, 8),
(1, 2, 4, 5),
(1, 2, 4, 6),
(1, 2, 4, 7),
(1, 2, 4, 8),
(1, 2, 5, 6),
(1, 2, 5, 7),
(1, 2, 5, 8),
(1, 2, 6, 7),
(1, 2, 6, 8),
(1, 2, 7, 8),
(1, 3, 4, 5),
(1, 3, 4, 6),
(1, 3, 4, 7),
(1, 3, 4, 8),
(1, 3, 5, 6),
(1, 3, 5, 7),
(1, 3, 5, 8),
(1, 3, 6, 7),
(1, 3, 6, 8),
(1, 3, 7, 8),
(1, 4, 5, 6),
(1, 4, 5, 7),
(1, 4, 5, 8),
(1, 4, 6, 7),
(1, 4, 6, 8),
(1, 4, 7, 8),
(1, 5, 6, 7),
(1, 5, 6, 8),
(1, 5, 7, 8),
(1, 6, 7, 8),
(2, 3, 4, 5),
(2, 3, 4, 6),
(2, 3, 4, 7),
(2, 3, 4, 8),
(2, 3, 5, 6),
(2, 3, 5, 7),
(2, 3, 5, 8),
(2, 3, 6, 7),
(2, 3, 6, 8),
(2, 3, 7, 8),
(2, 4, 5, 6),
(2, 4, 5, 7),
(2, 4, 5, 8),
(2, 4, 6, 7),
(2, 4, 6, 8),
(2, 4, 7, 8),
(2, 5, 6, 7),
(2, 5, 6, 8),
(2, 5, 7, 8),
(2, 6, 7, 8),
(3, 4, 5, 6),
(3, 4, 5, 7),
(3, 4, 5, 8),
(3, 4, 6, 7),
(3, 4, 6, 8),
(3, 4, 7, 8),
(3, 5, 6, 7),
(3, 5, 6, 8),
(3, 5, 7, 8),
(3, 6, 7, 8),
(4, 5, 6, 7),
(4, 5, 6, 8),
(4, 5, 7, 8),
(4, 6, 7, 8),
(5, 6, 7, 8)]

Related

How to convert list comprehension to for loop in python [duplicate]

This question already has answers here:
What does "list comprehension" and similar mean? How does it work and how can I use it?
(5 answers)
Closed 1 year ago.
So I have this list comprehension line I need to convert to a for loop in python.
pred and exp are lists that have many different values in them. They are part of a data set of numbers and with machine learning the code is supposed to guess the number(but that is just background)
correct = [(p, e) for (p, e) in zip(pred, exp) if p != e]
Output: of correct
[(8, 2),
(7, 2),
(9, 8),
(4, 0),
(2, 8),
(9, 7),
(8, 9),
(8, 9),
(2, 8),
(1, 8),
(4, 9),
(9, 4),
(3, 8),
(1, 8),
(3, 9),
(9, 5),
(9, 5),
(9, 7),
(3, 8),
(1, 8),
(9, 7),
(9, 7),
(2, 8),
(9, 7),
(3, 9),
(8, 2),
(1, 8),
(7, 3),
(1, 8),
(3, 8),
(9, 7),
(5, 9),
(3, 8),
(2, 8),
(8, 1),
(9, 1),
(1, 2),
(9, 5),
(8, 2),
(4, 5),
(9, 5),
(4, 6),
(2, 3),
(1, 8),
(8, 2),
(9, 3),
(8, 2),
(5, 3),
(9, 5),
(1, 8),
(1, 8),
(3, 9),
(8, 2),
(1, 9),
(1, 9),
(8, 1),
(3, 8),
(2, 7)]
I tried this
correct = []
for (p, e) in zip(predicted, expected):
if p != e:
correct.append(p)
correct.append(e)
print(correct)
But the output looks like this
[5, 3, 6, 8, 1, 4, 7, 9, 7, 3, 7, 8, 7, 9, 7, 4, 7, 9, 9, 8, 9, 8, 6, 8, 8, 2, 8, 2, 3, 8, 1, 8, 1, 2, 1, 8, 1, 8, 9, 5, 7, 9, 7, 9, 1, 8, 9, 5, 1, 8, 8, 2, 7, 2, 8, 9, 3, 8, 9, 5, 8, 9, 6, 3, 7, 9, 8, 6, 0, 2, 2, 8, 9, 5, 9, 5, 7, 4, 9, 5, 1, 8, 1, 8, 5, 9, 1, 2, 7, 4, 8, 2]
Any help would be appreciated, thanks
You want a list of tuples, but you get a list of numbers. The reason is that you are appending the numbers just as numbers, not as tuples.
Instead, do
if p != e:
correct.append((p,e))
Inside the for loop you're appending p and e to the list as single elements, despite the fact that on each iteration you get them inside a tuple.
To fix it just append them to the list as a tuple element.
if p != e:
correct.append((p,e))

Combinations of digits in a URL

I'm currently trying to iterate over all combinations of 4 digits on a URL but it seems to be only doing a handful of them (instead of 10,000). Any ideas why?
import requests
from itertools import combinations
x = [1,2,3,4,5,6,7,8,9,0]
for c in combinations(x, 4):
print(c)
URL = "http://www.google.com/" + str(c)
r = requests.get(url = URL)
print(r.content)
It only does the following permutations:
(1, 2, 3, 4)
(1, 2, 3, 5)
(1, 2, 3, 6)
(1, 2, 3, 7)
(1, 2, 3, 8)
(1, 2, 3, 9)
(1, 2, 3, 0)
(1, 2, 4, 5)
(1, 2, 4, 6)
(1, 2, 4, 7)
(1, 2, 4, 8)
(1, 2, 4, 9)
(1, 2, 4, 0)
(1, 2, 5, 6)
(1, 2, 5, 7)
(1, 2, 5, 8)
(1, 2, 5, 9)
(1, 2, 5, 0)
(1, 2, 6, 7)
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6, 8)
(4, 5, 6, 9)
(4, 5, 6, 0)
(4, 5, 7, 8)
(4, 5, 7, 9)
(4, 5, 7, 0)
(4, 5, 8, 9)
(4, 5, 8, 0)
(4, 5, 9, 0)
(4, 6, 7, 8)
(4, 6, 7, 9)
(4, 6, 7, 0)
(4, 6, 8, 9)
(4, 6, 8, 0)
(4, 6, 9, 0)
(4, 7, 8, 9)
(4, 7, 8, 0)
(4, 7, 9, 0)
(4, 8, 9, 0)
(5, 6, 7, 8)
(5, 6, 7, 9)
(5, 6, 7, 0)
(5, 6, 8, 9)
(5, 6, 8, 0)
(5, 6, 9, 0)
(5, 7, 8, 9)
(5, 7, 8, 0)
(5, 7, 9, 0)
(5, 8, 9, 0)
(6, 7, 8, 9)
(6, 7, 8, 0)
(6, 7, 9, 0)
(6, 8, 9, 0)
(7, 8, 9, 0)
Thanks!
Combination is ordered, you can easily see what it produces if you reduce the input:
x = [1,2,3,4]
for c in combinations(x, 2):
print(c)
Output:
(1, 2) (1, 3) (1, 4) # all combinations of 1 and any other number
(2, 3) (2, 4) # (1,2) was already done, so not repeated
(3, 4) # (1,3) and (2,3) already done, so not repeated
You find more details in the documentation.
There is also no reason to do itertools.combinations at all.
Use for c in range(1000,10000): instead. This will create all numbers from 1000 up to 9999

Dynamic nested loop - recursion [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 3 years ago.
Improve this question
I would like to generate a list of numbers who's digits are in ascending order.
for x in range(1,10):
for y in range(x,10):
for z in range(y,10):
print(x,y,z)
Is it possible for me to convert this probably to a recursion such that I can vary the nesting depth ?
Notes:
In my application I have something other than printing and I'd like to keep minimum burden on the print statement.
I am aware of the itertools.product and it's not suitable for my purpose as I have to remove the unnecessary combinations later.
Due to the same reason generating all n digit numbers and removing the unnecessary ones won't work as well
Thanks
You need itertools.combinations_with_replacement()
>>> import itertools
>>> list(itertools.combinations_with_replacement(range(1, 10), 2))
[(1, 1), (1, 2), (1, 3), (1, 4), (1, 5), (1, 6), (1, 7), (1, 8), (1, 9), (2, 2), (2, 3), (2, 4), (2, 5), (2, 6), (2, 7), (2, 8), (2, 9), (3, 3), (3, 4), (3, 5), (3, 6), (3, 7), (3, 8), (3, 9), (4, 4), (4, 5), (4, 6), (4, 7), (4, 8), (4, 9), (5, 5), (5, 6), (5, 7), (5, 8), (5, 9), (6, 6), (6, 7), (6, 8), (6, 9), (7, 7), (7, 8), (7, 9), (8, 8), (8, 9), (9, 9)]
>>> list(itertools.combinations_with_replacement(range(1, 10), 3))
[(1, 1, 1), (1, 1, 2), (1, 1, 3), (1, 1, 4), (1, 1, 5), (1, 1, 6), (1, 1, 7), (1, 1, 8), (1, 1, 9), (1, 2, 2), (1, 2, 3), (1, 2, 4), (1, 2, 5), (1, 2, 6), (1, 2, 7), (1, 2, 8), (1, 2, 9), (1, 3, 3), (1, 3, 4), (1, 3, 5), (1, 3, 6), (1, 3, 7), (1, 3, 8), (1, 3, 9), (1, 4, 4), (1, 4, 5), (1, 4, 6), (1, 4, 7), (1, 4, 8), (1, 4, 9), (1, 5, 5), (1, 5, 6), (1, 5, 7), (1, 5, 8), (1, 5, 9), (1, 6, 6), (1, 6, 7), (1, 6, 8), (1, 6, 9), (1, 7, 7), (1, 7, 8), (1, 7, 9), (1, 8, 8), (1, 8, 9), (1, 9, 9), (2, 2, 2), (2, 2, 3), (2, 2, 4), (2, 2, 5), (2, 2, 6), (2, 2, 7), (2, 2, 8), (2, 2, 9), (2, 3, 3), (2, 3, 4), (2, 3, 5), (2, 3, 6), (2, 3, 7), (2, 3, 8), (2, 3, 9), (2, 4, 4), (2, 4, 5), (2, 4, 6), (2, 4, 7), (2, 4, 8), (2, 4, 9), (2, 5, 5), (2, 5, 6), (2, 5, 7), (2, 5, 8), (2, 5, 9), (2, 6, 6), (2, 6, 7), (2, 6, 8), (2, 6, 9), (2, 7, 7), (2, 7, 8), (2, 7, 9), (2, 8, 8), (2, 8, 9), (2, 9, 9), (3, 3, 3), (3, 3, 4), (3, 3, 5), (3, 3, 6), (3, 3, 7), (3, 3, 8), (3, 3, 9), (3, 4, 4), (3, 4, 5), (3, 4, 6), (3, 4, 7), (3, 4, 8), (3, 4, 9), (3, 5, 5), (3, 5, 6), (3, 5, 7), (3, 5, 8), (3, 5, 9), (3, 6, 6), (3, 6, 7), (3, 6, 8), (3, 6, 9), (3, 7, 7), (3, 7, 8), (3, 7, 9), (3, 8, 8), (3, 8, 9), (3, 9, 9), (4, 4, 4), (4, 4, 5), (4, 4, 6), (4, 4, 7), (4, 4, 8), (4, 4, 9), (4, 5, 5), (4, 5, 6), (4, 5, 7), (4, 5, 8), (4, 5, 9), (4, 6, 6), (4, 6, 7), (4, 6, 8), (4, 6, 9), (4, 7, 7), (4, 7, 8), (4, 7, 9), (4, 8, 8), (4, 8, 9), (4, 9, 9), (5, 5, 5), (5, 5, 6), (5, 5, 7), (5, 5, 8), (5, 5, 9), (5, 6, 6), (5, 6, 7), (5, 6, 8), (5, 6, 9), (5, 7, 7), (5, 7, 8), (5, 7, 9), (5, 8, 8), (5, 8, 9), (5, 9, 9), (6, 6, 6), (6, 6, 7), (6, 6, 8), (6, 6, 9), (6, 7, 7), (6, 7, 8), (6, 7, 9), (6, 8, 8), (6, 8, 9), (6, 9, 9), (7, 7, 7), (7, 7, 8), (7, 7, 9), (7, 8, 8), (7, 8, 9), (7, 9, 9), (8, 8, 8), (8, 8, 9), (8, 9, 9), (9, 9, 9)]
Here, You have to pass the list of numbers as well as depth, so that you can constantly increse the list with each recursion.
def get_nums(depth=0,pre_list=[[1]]):
if depth==0:
return pre_list
new_list=[]
for num in pre_list:
n=num[-1]
for i in range(n,10):
new_list.append(num+[i])
return get_nums(depth-1,new_list)
print(get_nums(3))
Here's a recursive solution,
def rec(s,idx,num):
if idx>=len(s)-1:
print(s)
return
for i in range(num,10):
s[idx+1] = i # change next number
rec(s,idx+1,i) # recurse
s = [1,1,1]
idx = -1
num = s[0]
rec(s,idx,num)
Output
[1, 1, 1]
[1, 1, 2]
[1, 1, 3]
[1, 1, 4]
[1, 1, 5]
[1, 1, 6]
[1, 1, 7]
[1, 1, 8]
[1, 1, 9]
[1, 2, 2]
[1, 2, 3]
[1, 2, 4]
[1, 2, 5]
[1, 2, 6]
[1, 2, 7]
[1, 2, 8]
[1, 2, 9]
[1, 3, 3]
...and so on until [9,9,9]

How to create a list of all possible combinations of 3 lists? [duplicate]

This question already has answers here:
How to get the cartesian product of multiple lists
(17 answers)
Closed 3 years ago.
If I have 3 list [0, 1, 2], [3, 4, 5], [6, 7, 8], how would I obtain an output of a list of lists that are of length three and contain one element from each list and has all possible combinations?
For example, one of the possible combinations of the above is [1, 5, 6].
I tried using numpy.meshgrid, but that only gave me some possible combinations and not all.
import itertools
x = [[0, 1, 2], [3, 4, 5], [6, 7, 8]]
all_combinations = list(itertools.product(*x))
[(0, 3, 6),
(0, 3, 7),
(0, 3, 8),
(0, 4, 6),
(0, 4, 7),
(0, 4, 8),
(0, 5, 6),
(0, 5, 7),
(0, 5, 8),
(1, 3, 6),
(1, 3, 7),
(1, 3, 8),
(1, 4, 6),
(1, 4, 7),
(1, 4, 8),
(1, 5, 6),
(1, 5, 7),
(1, 5, 8),
(2, 3, 6),
(2, 3, 7),
(2, 3, 8),
(2, 4, 6),
(2, 4, 7),
(2, 4, 8),
(2, 5, 6),
(2, 5, 7),
(2, 5, 8)]

python combinations of multiple list

Is there any pythonic method to generate combinations between multiple list? (similar to Cartesian product but more complicated)
Example:
a = [1, 2, 3]
b = [4, 5, 6]
c = [7, 8, 9]
# ...
# there are more than 3 lists
Expected output:
1. [(1, 4, 7), (2, 5, 8), (3, 6, 9)]
2. [(1, 4, 8), (2, 5, 7), (3, 6, 9)]
3. [(1, 4, 9), (2, 5, 7), (3, 6, 8)]
4. [(1, 5, 7), (2, 4, 8), (3, 6, 9)]
5. ...
Update:
Thanks for the quick reply~!!
To clarify the question:
The result are all non-repeated combinations of Cartesian product of list a, b, c.
It can be done by another ugly method:
1) Generate the whole list of Cartesian product
from itertools import product, combinations, chain
t = list(product(a, b, c))
2) Using combinations to generate all possible results
p = list(combinations(t, 3))
3) Filter the repeated conditions
cnt = len(list(chain(a, b, c)))
f = [x for x in p if len(set(chain(*x))) == cnt]
Update2:
Expected result generated by ugly method:
((1, 4, 7), (2, 5, 8), (3, 6, 9))
((1, 4, 7), (2, 5, 9), (3, 6, 8))
((1, 4, 7), (2, 6, 8), (3, 5, 9))
((1, 4, 7), (2, 6, 9), (3, 5, 8))
((1, 4, 8), (2, 5, 7), (3, 6, 9))
((1, 4, 8), (2, 5, 9), (3, 6, 7))
((1, 4, 8), (2, 6, 7), (3, 5, 9))
((1, 4, 8), (2, 6, 9), (3, 5, 7))
((1, 4, 9), (2, 5, 7), (3, 6, 8))
((1, 4, 9), (2, 5, 8), (3, 6, 7))
((1, 4, 9), (2, 6, 7), (3, 5, 8))
((1, 4, 9), (2, 6, 8), (3, 5, 7))
((1, 5, 7), (2, 4, 8), (3, 6, 9))
((1, 5, 7), (2, 4, 9), (3, 6, 8))
((1, 5, 7), (2, 6, 8), (3, 4, 9))
((1, 5, 7), (2, 6, 9), (3, 4, 8))
((1, 5, 8), (2, 4, 7), (3, 6, 9))
((1, 5, 8), (2, 4, 9), (3, 6, 7))
((1, 5, 8), (2, 6, 7), (3, 4, 9))
((1, 5, 8), (2, 6, 9), (3, 4, 7))
((1, 5, 9), (2, 4, 7), (3, 6, 8))
((1, 5, 9), (2, 4, 8), (3, 6, 7))
((1, 5, 9), (2, 6, 7), (3, 4, 8))
((1, 5, 9), (2, 6, 8), (3, 4, 7))
((1, 6, 7), (2, 4, 8), (3, 5, 9))
((1, 6, 7), (2, 4, 9), (3, 5, 8))
((1, 6, 7), (2, 5, 8), (3, 4, 9))
((1, 6, 7), (2, 5, 9), (3, 4, 8))
((1, 6, 8), (2, 4, 7), (3, 5, 9))
((1, 6, 8), (2, 4, 9), (3, 5, 7))
((1, 6, 8), (2, 5, 7), (3, 4, 9))
((1, 6, 8), (2, 5, 9), (3, 4, 7))
((1, 6, 9), (2, 4, 7), (3, 5, 8))
((1, 6, 9), (2, 4, 8), (3, 5, 7))
((1, 6, 9), (2, 5, 7), (3, 4, 8))
((1, 6, 9), (2, 5, 8), (3, 4, 7))
What you want are not combinations but indeed permutations. 3 elements have 6 permutations, a Cartesian product of 2 sets of permutations has 36. PM 2Ring originally suspected that you want all 3 of these permuted since your question didn't tell otherwise. If the code in your question produces the desired output, it means you want b and c permuted but not a. Initially I wrote code that calculated the permutations for all of a, b and c. However, since a doesn't need to be permuted, we'll just wrap it in a list. This gets us very close to the desired output:
import itertools as it
a = [1, 2, 3]
b = [4, 5, 6]
c = [7, 8, 9]
for i in it.product([tuple(a)], it.permutations(b), it.permutations(c)):
print(i)
The output is 36 lines that start with
((1, 2, 3), (4, 5, 6), (7, 8, 9))
((1, 2, 3), (4, 5, 6), (7, 9, 8))
((1, 2, 3), (4, 5, 6), (8, 7, 9))
It is already almost the same format that you want but with indexes transposed so o[x][y] would match o[y][x] of your desired output. We use some zip magic to transpose them. As a plus, this function now works for any number of arguments:
import itertools as it
a = [1, 2, 3]
b = [4, 5, 6]
c = [7, 8, 9]
def funnyperms(first, *rest):
for i in it.product([first], *(it.permutations(j) for j in rest)):
yield tuple(zip(*i))
for i in funnyperms(a, b, c):
print(i)
The output is
((1, 4, 7), (2, 5, 8), (3, 6, 9))
((1, 4, 7), (2, 5, 9), (3, 6, 8))
((1, 4, 8), (2, 5, 7), (3, 6, 9))
((1, 4, 8), (2, 5, 9), (3, 6, 7))
((1, 4, 9), (2, 5, 7), (3, 6, 8))
((1, 4, 9), (2, 5, 8), (3, 6, 7))
((1, 4, 7), (2, 6, 8), (3, 5, 9))
((1, 4, 7), (2, 6, 9), (3, 5, 8))
((1, 4, 8), (2, 6, 7), (3, 5, 9))
((1, 4, 8), (2, 6, 9), (3, 5, 7))
((1, 4, 9), (2, 6, 7), (3, 5, 8))
((1, 4, 9), (2, 6, 8), (3, 5, 7))
((1, 5, 7), (2, 4, 8), (3, 6, 9))
((1, 5, 7), (2, 4, 9), (3, 6, 8))
((1, 5, 8), (2, 4, 7), (3, 6, 9))
((1, 5, 8), (2, 4, 9), (3, 6, 7))
((1, 5, 9), (2, 4, 7), (3, 6, 8))
((1, 5, 9), (2, 4, 8), (3, 6, 7))
((1, 5, 7), (2, 6, 8), (3, 4, 9))
((1, 5, 7), (2, 6, 9), (3, 4, 8))
((1, 5, 8), (2, 6, 7), (3, 4, 9))
((1, 5, 8), (2, 6, 9), (3, 4, 7))
((1, 5, 9), (2, 6, 7), (3, 4, 8))
((1, 5, 9), (2, 6, 8), (3, 4, 7))
((1, 6, 7), (2, 4, 8), (3, 5, 9))
((1, 6, 7), (2, 4, 9), (3, 5, 8))
((1, 6, 8), (2, 4, 7), (3, 5, 9))
((1, 6, 8), (2, 4, 9), (3, 5, 7))
((1, 6, 9), (2, 4, 7), (3, 5, 8))
((1, 6, 9), (2, 4, 8), (3, 5, 7))
((1, 6, 7), (2, 5, 8), (3, 4, 9))
((1, 6, 7), (2, 5, 9), (3, 4, 8))
((1, 6, 8), (2, 5, 7), (3, 4, 9))
((1, 6, 8), (2, 5, 9), (3, 4, 7))
((1, 6, 9), (2, 5, 7), (3, 4, 8))
((1, 6, 9), (2, 5, 8), (3, 4, 7))
Storing these into a set and comparing with the values produced by your method proves that they have identical output:
print(set(funnyperms(a, b, c)) == set(f))
prints True, Q.E.D.
You can use product from itertools for all combinations
>>> from itertools import product
>>> a = [1, 2, 3]
>>> b = [4, 5, 6]
>>> c = [7, 8, 9]
>>> A = [a,b,c]
>>> prod = list(product(*A))
>>> print(prod)
Expected output:
[(1, 4, 7), (1, 4, 8), (1, 4, 9), (1, 5, 7), (1, 5, 8), (1, 5, 9), (1, 6, 7), (1, 6, 8), (1, 6, 9), (2, 4, 7), (2, 4, 8), (2, 4, 9), (2, 5, 7), (2, 5, 8), (2, 5, 9), (2, 6, 7), (2, 6, 8), (2, 6, 9), (3, 4, 7), (3, 4, 8), (3, 4, 9), (3, 5, 7), (3, 5, 8), (3, 5, 9), (3, 6, 7), (3, 6, 8), (3, 6, 9)]

Categories