I'm trying to create an iterator/generator of all variable length strings given an alphabet and a maximum string length, sorted in lexicographic order.
Currently, I have a naive method that uses nested itertools product(), then proceeds to sort. This works great for small max_len_string, but for my target usage (around max_len_string=32) this uses far too much temporary storage to be practical.
Is there a way to make this algorithm use only a small amount of constant space each iteration instead of slurping the entire sequence in sorting?
from itertools import product
def variable_strings_complete(max_len_string, alphabet=range(2)):
yield from sorted(string
for i in range(1, max_len_string+1)
for string in product(alphabet, repeat=i))
list(variable_strings_complete(3))
[(0,),
(0, 0),
(0, 0, 0),
(0, 0, 1),
(0, 1),
(0, 1, 0),
(0, 1, 1),
(1,),
(1, 0),
(1, 0, 0),
(1, 0, 1),
(1, 1),
(1, 1, 0),
(1, 1, 1)]