Advertisement
brainuser5705

permutations

Apr 20th, 2021
112
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.99 KB | None | 0 0
  1. permutations = []
  2. letters = ['a','b','c']
  3.  
  4. def find_permutations(config):
  5.     successors = get_successors(config)
  6.     for successor in successors:
  7.         if (len(successor) == 3):
  8.             permutations.append(successor)
  9.         find_permutations(successor) # still find the successors
  10.  
  11.  
  12. def get_successors(config):
  13.     successors = []
  14.     for letter in letters:
  15.         if (letter not in config):
  16.             successors.append(config+letter)
  17.     return successors
  18.  
  19. ##other solution:
  20.  
  21. def backtracking(config, options):
  22.     if len(config) == len(options): # add to array
  23.         permutations.append(config[:])
  24.     else:
  25.         for option in options: # get the sucessors and backtrack them
  26.             if option in config:
  27.                 continue
  28.             config.append(option)
  29.             backtracking(config, options)
  30.             config.pop() # for new config
  31.                
  32.  
  33. # run              
  34. backtracking([], letters)
  35. find_permutations('')
  36. print(permutations)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement