Last active
October 12, 2016 04:19
-
-
Save Jamesits/2a48f6a37eaf09df219ef862fbc6da86 to your computer and use it in GitHub Desktop.
Recursive string permutation in Python (2&3 compatible)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #!/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