Find All Possible Fixed Size String Python
Solution 1:
You can use itertools.product for this. It returns a generator of sequences of fixed length.
As you order your strings first by length and then lexicographically, you may use something like this
import itertools
for l in range(1, 5):
forseqin itertools.product("abc", repeat=l):
print("".join(seq))
Solution 2:
Using recursion, regardless of algorithm, might be the simplest way to go. Unfortunately you are right, it could use a lot of memory opening up each frame as it recurs down. You might even hit the recursion limit.
I would check out Continuation Passing Style. It's an optimization that allows for fewer frames to be used when doing large recursive work like this. It would not hit the recursion limit and probably speed things up. I'm pretty sure that Python supports this optimization. It's an advanced topic, so it might take some time to make work.
http://baruchel.github.io/blog/python/2015/07/10/continuation-passing-style-in-python/
Post a Comment for "Find All Possible Fixed Size String Python"