The collection of combinations that you want of your key letters A
, J
, and K
is called the
Cartesian product.
In Python, you can use
itertools.product
to generate them.
Firstly, we need to find the positions of all of the key letters in the input string. The easy way to do that uses the built-in
enumerate
function. Once I know those positions, and how many key letters the string contains, we can generate each item of the Cartesian product, replace the key letters, and print the new string.
In Python, strings are immutable (they can't be changed), so I convert the string to a list of characters, replace the characters in the key positions, and then build a new string from the list using the
str.join
method.
The following code will work with both versions 2 and 3 of Python
Python
from itertools import product
def make_patterns(s):
keyletters = 'AJK'
# Convert input string into a list so we can easily substitute letters
seq = list(s)
# Find indices of key letters in seq
indices = [ i for i, c in enumerate(seq) if c in keyletters ]
# Generate key letter combinations & place them into the list
for t in product(keyletters, repeat=len(indices)):
for i, c in zip(indices, t):
seq[i] = c
print(''.join(seq))
# Test
data = (
'1ABC2',
'27AAGCB5913L2ZF',
'3A4J',
'5K67KA',
)
for s in data:
print('\nInput:', s)
make_patterns(s)
output
Input: 1ABC2
1ABC2
1JBC2
1KBC2
Input: 27AAGCB5913L2ZF
27AAGCB5913L2ZF
27AJGCB5913L2ZF
27AKGCB5913L2ZF
27JAGCB5913L2ZF
27JJGCB5913L2ZF
27JKGCB5913L2ZF
27KAGCB5913L2ZF
27KJGCB5913L2ZF
27KKGCB5913L2ZF
Input: 3A4J
3A4A
3A4J
3A4K
3J4A
3J4J
3J4K
3K4A
3K4J
3K4K
Input: 5K67KA
5A67AA
5A67AJ
5A67AK
5A67JA
5A67JJ
5A67JK
5A67KA
5A67KJ
5A67KK
5J67AA
5J67AJ
5J67AK
5J67JA
5J67JJ
5J67JK
5J67KA
5J67KJ
5J67KK
5K67AA
5K67AJ
5K67AK
5K67JA
5K67JJ
5K67JK
5K67KA
5K67KJ
5K67KK
With a minor change, we can turn our function into a generator. That lets you loop over the output strings easily, or turn them into a list if you want.
Python
from itertools import product
def make_patterns(s):
keyletters = 'AJK'
# Convert input string into a list so we can easily substitute letters
seq = list(s)
# Find indices of key letters in seq
indices = [i for i, c in enumerate(seq) if c in keyletters]
# Generate key letter combinations & place them into the list
for t in product(keyletters, repeat=len(indices)):
for i, c in zip(indices, t):
seq[i] = c
yield ''.join(seq)
# Test
print(list(make_patterns('A12K')))
for s in make_patterns('3KJ4'):
print(s)
output
['A12A', 'A12J', 'A12K', 'J12A', 'J12J', 'J12K', 'K12A', 'K12J', 'K12K']
3AA4
3AJ4
3AK4
3JA4
3JJ4
3JK4
3KA4
3KJ4
3KK4