Sample dataset:
2
Expected output:
8
-1 -2
-1 2
1 -2
1 2
-2 -1
-2 1
2 -1
2 1
Note that we should not count permutations of the same integer (i. e. -1 1, 2 -1 and so on should not be included).
For this problem we can once again utilise the itertools function permutations. However, this time we need to pair it to the itertools function product. The following is my code:
import itertools
n = 3
permutation = []
nr = 0
for i in itertools.permutations(list(range(1, n + 1))):
for j in itertools.product([-1, 1], repeat=len(list(range(1, n + 1)))):
perm = [a * sign for a, sign in zip(i, j)]
permutation.append(perm)
nr += 1
print(nr)
for i in range(len(permutation)):
print(*permutation[i], sep=' ')
No comments:
Post a Comment