Skip to content

Instantly share code, notes, and snippets.

@Jamesits
Last active October 12, 2016 04:19
Show Gist options
  • Select an option

  • Save Jamesits/2a48f6a37eaf09df219ef862fbc6da86 to your computer and use it in GitHub Desktop.

Select an option

Save Jamesits/2a48f6a37eaf09df219ef862fbc6da86 to your computer and use it in GitHub Desktop.
Recursive string permutation in Python (2&3 compatible)
#!/usr/bin/env python
from __future__ import print_function
def get_permutation(mystr):
# base case
if len(mystr) <= 1:
return [mystr]
# recursion
else:
ret = []
for i in range(0, len(mystr)):
ret += [
item[:i] + mystr[0] + item[i:]
for item in get_permutation(mystr[1:])
]
return list(set(ret))
# test case
if __name__ == '__main__':
print(get_permutation("bust"))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment