Advertisement
Guest User

Untitled

a guest
May 19th, 2017
72
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.01 KB | None | 0 0
  1.  
  2. # Inserting symbol in the string in specified position
  3. def insert_symbol(str, c, pos):
  4.     return str[:pos] + c + str[pos:]
  5.  
  6. # Generate sequence of strings, obtained as addition of an element to seqence
  7. def insert_everywhere(str, c):
  8.     return map(lambda n: insert_symbol(str, c, n), range(len(str) + 1))
  9.  
  10. def generatePermutations(s):
  11.     if s == '':
  12.         return []
  13.     # Remove last symbol
  14.     cut = s[:-1]
  15.     last_symbol = s[-1]
  16.     # Get all permutation of cutted string
  17.     cut_permutations = generatePermutations(cut)
  18.     # These permutations will also go to answer
  19.     permutated_list = cut_permutations
  20.     # Get list of all insertions of the symbol to string
  21.     insertions_lists = map(lambda perm: insert_everywhere(perm, last_symbol), permutated_list)
  22.     # Append all possible insertions to target list
  23.     for insertion in insertions_lists:
  24.         permutated_list += insertion
  25.     # Add permutation from standalone last symbol
  26.     permutated_list += [last_symbol]
  27.     return permutated_list
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement