Thread Rating:
  • 2 Vote(s) - 2 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Generating all permutations of an array of m different elements.
#1
Code:
def allPerms(m):
    ordSet = [i for i in range(m)]
    perm, perms = [], []
    def genPerms(ordSet, m=m):
        nonlocal perm, perms
        for i in ordSet:
            perm += [i]
            ordSet2 = ordSet.copy()
            ordSet2.remove(i)
            genPerms(ordSet2)
        if len(perm) == m:
            perms += [perm]
        perm = perm[:-1]
    genPerms(ordSet)
    return perms
Reply
#2
[Image: ?u=http%3A%2F%2Fwww.reactiongifs.com%2Fr...f=1&nofb=1]
Reply


Forum Jump:


Users browsing this thread:
1 Guest(s)