Let's see how to achieve the proposed result:
def pattern( char1, char2, n ):
# first we save the base pattern
out = char1 + char2
for i in range( n ):
# in each iteration, we add to it its inverted version
out += invert( out )
return out
def invert( char ):
out = ""
for i in range( len( char )):
# only in the odd numbers we add to **out** the reversed items
if i % 2 != 0:
out += char[ i ] + char[ i - 1 ]
return out