patternpythonMinor
Showing all possible permutations from a set
Viewed 0 times
showingallpossiblefromsetpermutations
Problem
I wrote a small program which shows all possible permutations of choosing m integers from the set \${1, 2, ..., n}\$, as follows:
However, this code is inefficient. Perhaps the recursive structure is not well-designed. How can I improve this code?
def perm(nlist, m):
"""
Find all possible permutations of choosing m integers from a len(n)-list.
INPUT:
@para nlist: a list of length n.
@para m: a integer.
OUTPUT:
All permutations arranged in ascending order lexicographically.
"""
if m > len(nlist):
return
if m == 0 or m == len(nlist):
return [[]]
results = []
for list_i in nlist:
temp = list(nlist)
temp.remove(list_i)
results.extend(map(lambda x: [list_i] + x, perm(temp, m-1)))
return resultsHowever, this code is inefficient. Perhaps the recursive structure is not well-designed. How can I improve this code?
Solution
As noted by 200_success in the comments, the
Also,
Turning the function into a generator simplifies it and speeds it up at the same time: from 10.9 ms to 6.6 ms for the case
m == len(nlist) check is a bug that can be fixed by simply removing the condition.Also,
if m > len(nlist): return is almost redundant because you would get an empty list as the result anyway in that case. (The deepest levels of recursion give empty results when the list runs out and the emptiness propagates all the way up)Turning the function into a generator simplifies it and speeds it up at the same time: from 10.9 ms to 6.6 ms for the case
perm(range(6), 6). (Of course I wrapped that in list() when timing the generator version)def perm(nlist, m):
if m == 0:
yield []
return
for list_i in nlist:
temp = list(nlist)
temp.remove(list_i)
for p in perm(temp, m-1):
yield [list_i] + pCode Snippets
def perm(nlist, m):
if m == 0:
yield []
return
for list_i in nlist:
temp = list(nlist)
temp.remove(list_i)
for p in perm(temp, m-1):
yield [list_i] + pContext
StackExchange Code Review Q#31665, answer score: 3
Revisions (0)
No revisions yet.