I am making a function that takes in a string and returns all possibilities of outputs, using the following transformations:
o -> 0 i,l -> 1
z -> 2 e -> 3
a -> 4 s -> 5
b -> 6 t -> 7
b -> 8 g,q -> 9
For example:
print(transform_digits("Bow"))
['Bow', 'B0w', '6ow', '60w', '8ow', '80w']
Here is the helper function that I use to get a transformation of a string st
at an index i
:
def position_transform(st,i):
letter=st[i]
front = st[:i]
back=st[(i+1):]
l=[]
if (letter=="o" or letter=="O"): l.append(front+"0"+back)
elif (letter=="z" or letter=="Z"): l.append(front+"2"+back)
elif (letter=="a" or letter=="A"): l.append(front+"4"+back)
elif (letter=="b" or letter=="B"):
l.append(front+"6"+back)
l.append(front+"8"+back)
elif (letter=="i" or letter=="I"or letter=="l" or letter =="L"): l.append(front+"1"+back)
elif (letter=="e" or letter=="E"): l.append(front+"3"+back)
elif (letter=="s" or letter=="S"): l.append(front+"5"+back)
elif (letter=="t" or letter=="T"): l.append(front+"7"+back)
elif (letter=="g" or letter=="G"or letter=="q" or letter =="Q"): l.append(front+"9"+back)
return l
Here is the main function:
def transform_digits(st):
l=[]
l.append(st)
newl=l[:]
length=len(st)
for x in range(0,length):
for i in l:
s=position_transform(i,x)
newl.append(s)
l=newl[:]
return l
when I run the main function I get:
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "<stdin>", line 8, in transform_digits
File "<stdin>", line 2, in position_transform
IndexError: list index out of range
I'm not exactly sure where this error is. I've tried running position_transform
on strings and that seems to work out fine, and my logic for the other functions seems fine, not sure where the index error is.