Home > Back-end >  Special kind of permutation
Special kind of permutation

Time:12-20

I am working on some code to produce all possible permutations from a list. Such products are constrained exclusively to one specific size. My code works for regular permutations, but I do not know how to obtain permutations that follow this type of logic. I'm supposed to get all of the permutations that follow this sort of criss-cross pattern, where the first element is followed by another element and then repeated until the product reaches size z, into one big array. For example, given an array like [0,1,2,3] and z=3, One should return something like [[0,1,0][1,0,1][2,0,2][0,2,0][0,3,0][0,4,0]...] Of course the output would change based on z, so, if we had z=4 the array would have subarrays that are of size 4, like [[0,1,0,1],[1,0,1,0],[2,0,2,0],[0,2,0,2], etc.] Fundamentally, you just need to alternate two elements in the original array so they form that type of a-b/a-b-a/a-b-a-b/pattern, so two elements at a time are taken. This is my code for basic permutations with size z.

def comb(k,array):
if k<=0:
    return [[]]
else:
    final=[]
    for part in comb(k-1,array):
        for e in array:
            final.append([e] part)
        return final

I don't want to use any library because I am studying recursion specifically and that would be sort of pointless learning-wise. Thank you.

CodePudding user response:

Use itertools.product:

from itertools import product

lst = [0,1,2,3]

result = list(product(lst,repeat=3))
[(0, 0, 0), (0, 0, 1), (0, 0, 2), (0, 0, 3), (0, 1, 0), (0, 1, 1), (0, 1, 2), (0, 1, 3), (0, 2, 0), (0, 2, 1), (0, 2, 2), (0, 2, 3), (0, 3, 0), (0, 3, 1), (0, 3, 2), (0, 3, 3), (1, 0, 0), (1, 0, 1), (1, 0, 2), (1, 0, 3), (1, 1, 0), (1, 1, 1), (1, 1, 2), (1, 1, 3), (1, 2, 0), (1, 2, 1), (1, 2, 2), (1, 2, 3), (1, 3, 0), (1, 3, 1), (1, 3, 2), (1, 3, 3), (2, 0, 0), (2, 0, 1), (2, 0, 2), (2, 0, 3), (2, 1, 0), (2, 1, 1), (2, 1, 2), (2, 1, 3), (2, 2, 0), (2, 2, 1), (2, 2, 2), (2, 2, 3), (2, 3, 0), (2, 3, 1), (2, 3, 2), (2, 3, 3), (3, 0, 0), (3, 0, 1), (3, 0, 2), (3, 0, 3), (3, 1, 0), (3, 1, 1), (3, 1, 2), (3, 1, 3), (3, 2, 0), (3, 2, 1), (3, 2, 2), (3, 2, 3), (3, 3, 0), (3, 3, 1), (3, 3, 2), (3, 3, 3)]

CodePudding user response:

Try permutations from itertools module:

l = [0, 1, 2, 3]
z = 3
out = list(permutations(l, r=z))
print(out)

# Output:
[(0, 1, 2),
 (0, 1, 3),
 (0, 2, 1),
 (0, 2, 3),
 (0, 3, 1),
 (0, 3, 2),
 (1, 0, 2),
 (1, 0, 3),
 (1, 2, 0),
 (1, 2, 3),
 (1, 3, 0),
 (1, 3, 2),
 (2, 0, 1),
 (2, 0, 3),
 (2, 1, 0),
 (2, 1, 3),
 (2, 3, 0),
 (2, 3, 1),
 (3, 0, 1),
 (3, 0, 2),
 (3, 1, 0),
 (3, 1, 2),
 (3, 2, 0),
 (3, 2, 1)]

Check the code of permutations which is roughly equivalent.

For z=4:

l = [0, 1, 2, 3]
z = 4
out = list(permutations(l, r=z))
print(out)

# Output:
[(0, 1, 2, 3),
 (0, 1, 3, 2),
 (0, 2, 1, 3),
 (0, 2, 3, 1),
 (0, 3, 1, 2),
 (0, 3, 2, 1),
 (1, 0, 2, 3),
 (1, 0, 3, 2),
 (1, 2, 0, 3),
 (1, 2, 3, 0),
 (1, 3, 0, 2),
 (1, 3, 2, 0),
 (2, 0, 1, 3),
 (2, 0, 3, 1),
 (2, 1, 0, 3),
 (2, 1, 3, 0),
 (2, 3, 0, 1),
 (2, 3, 1, 0),
 (3, 0, 1, 2),
 (3, 0, 2, 1),
 (3, 1, 0, 2),
 (3, 1, 2, 0),
 (3, 2, 0, 1),
 (3, 2, 1, 0)]
  • Related