Skip to content Skip to sidebar Skip to footer

Is There A Function In Python Which Generates All The Strings Of Length N Over A Given Alphabet?

I need a function generateAllStrings(n, alphabet) to do something like this: generateAllStrings(4, ['a','b']) >>> ['aaaa', 'aaab', 'aaba', 'aabb', 'abaa', .... , 'bbba', '

Solution 1:

>>> [''.join(i) for i in itertools.product("ab",repeat=4)]
['aaaa', 'aaab', 'aaba', 'aabb', 'abaa', 'abab', 'abba', 'abbb', 'baaa', 'baab', 'baba', 'babb', 'bbaa', 'bbab', 'bbba', 'bbbb']

Post a Comment for "Is There A Function In Python Which Generates All The Strings Of Length N Over A Given Alphabet?"