Take two #
characters out of the input, generate permutations of what's left, and stick #
characters on each end of the generated permutations. Note that it's quite likely that you'll still have far too many permutations to ever go through.
By the way, you don't need to write your own permutation generator. The standard library provides itertools.permutations
, which is substantially faster than your code and doesn't have a few of the bugs your code does.
import itertools
def hash_on_each_end_permutations(array):
array = list(array)
array.remove('#')
array.remove('#')
for subpermutation in itertools.permutations(array):
yield '#' + ''.join(subpermutation) + '#'