For example, given the two letters A and B, I’d like to generate all strings of length n that have x A’s and y B’s.
I’d like this to be done efficiently. One way that I’ve considered is to build a length x list of A’s, and then insert y B’s into the list every possible way. But insertion into a python list is linear, so this method would suck as the list gets big.
PERFORMANCE GOAL (this may be unreasonable, but it is my hope): Generate all strings of length 20 with equal numbers of A and B in time less than a minute.
EDIT: Using permutations(‘A’ * x, ‘B’ * y) has been suggested. While not a bad idea, it’s wasting a lot. If x = y = 4, you’d generate the string ‘AAAABBBB’ many times. Is there a better way that might generate each string only once? I’ve tried code to the effect of set(permutations(‘A’ * x, ‘B’ * y)) and it is too slow.
Regarding your concerns with the performance, here is an actual generator implementation of your idea (without
insert). It finds the positions forBand fill the list accordingly.It does perform better. Comparing with the Greg Hewgill‘s solution (naming it
make_sequences2):Edit
A generalized version:
Usage: