How can I print all sequences of a given length using only 'O'
or 'Z'
in Python?
For example, if I give length = 3
the output would be:
'OOO'
'OOZ'
'OZO'
'OZZ'
'ZOO'
'ZZO'
'ZOZ'
'ZZZ'
How can I print all sequences of a given length using only 'O'
or 'Z'
in Python?
For example, if I give length = 3
the output would be:
'OOO'
'OOZ'
'OZO'
'OZZ'
'ZOO'
'ZZO'
'ZOZ'
'ZZZ'
You can achieve this by generating the cartesian product of the elements using itertools
:
import itertools
letters = ["O", "Z"]
length = 3
vals = [''.join(comb) for comb in itertools.product(letters, repeat=length)]
print(vals)
>>>['OOO', 'OOZ', 'OZO', 'OZZ', 'ZOO', 'ZOZ', 'ZZO', 'ZZZ']