2
\$\begingroup\$

I have written a Python one-liner that permutes a given string. I wrote this just for fun using list comprehensions and I would like to get feedback from the community.

def permute(c, s=""):
 return [s] if len(s) == len(c) else [x for y in [permute(c, e) for e in [s[0:i]+c[len(s)]+s[i:] for i in xrange(len(s)+1)]] for x in y]
print permute ("omer")
asked Feb 1, 2017 at 0:56
\$\endgroup\$

2 Answers 2

7
\$\begingroup\$

Even though the code fits a single line, this is not Pythonic. Just because Python allows to put a lot of things on a single line because of list comprehensions, ternary operators and other syntactic sugar, it does not mean you have to try making your code as short as possible. Remember the Zen of Python, in particular:

Explicit is better than implicit.

Flat is better than nested.

Readability counts.

If the implementation is hard to explain, it's a bad idea.

Expand the solution into multiple lines using meaningful variable names and comments:

def permute(c, s=""):
 """Returns all possible permutations of a string c."""
 if len(s) == len(c): # base case - returns a single variation of a string - string itself
 return [s]
 else:
 variations = [s[0:i] + c[len(s)] + s[i:] for i in range(len(s) + 1)]
 # get all permutations for each variation of a string
 permutations = [permute(c, e) for e in variations]
 # flatten a list of lists
 return [x for y in permutations for x in y]
print(permute("omer"))
answered Feb 1, 2017 at 1:50
\$\endgroup\$
1
  • 3
    \$\begingroup\$ +1. Thankfully OP didn't use a lambda to define it completely in a single line... \$\endgroup\$ Commented Feb 1, 2017 at 6:06
1
\$\begingroup\$

It looks like you are doing recursion and a list comprehension on the same line.

The recursion uses an optional s parameter as an accumulator, which "leaks" into the interface of the function. It would be cleaner to define a helper function within the function to hide that parameter.

The list comprehension is quite complicated — the term for that is incomprehensible comprehension. You would be better off writing a generator, yielding each result as you encounter it, instead of building one giant list.

If you want your code to be short, though, just use itertools.permutations().

answered Feb 4, 2017 at 19:08
\$\endgroup\$

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.