Skip to content

Instantly share code, notes, and snippets.

@hnq90
Created December 2, 2015 02:00
Show Gist options
  • Select an option

  • Save hnq90/0cb8d54df7ab95683a69 to your computer and use it in GitHub Desktop.

Select an option

Save hnq90/0cb8d54df7ab95683a69 to your computer and use it in GitHub Desktop.
def get_permutations(string):
# base case
if len(string) <= 1:
return [string]
all_chars_except_last = string[:-1]
last_char = string[-1]
# recursive call: get all possible permutations for all chars except last
permutations_of_all_chars_except_last = get_permutations(all_chars_except_last)
# put the last char in all possible positions for each of the above permutations
possible_positions_to_put_last_char = range(len(all_chars_except_last)+1)
permutations = set()
for permutation_of_all_chars_except_last in permutations_of_all_chars_except_last:
for position in possible_positions_to_put_last_char:
permutation = permutation_of_all_chars_except_last[:position] + last_char + permutation_of_all_chars_except_last[position:]
print permutation
permutations.add(permutation)
return permutations
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment