I was trying to write a function that would return the incrementation of a string. For example, if the input were "xx", it should return [xx, xy, xz, ya, yb, ..., zz, aa]. The function for incrementing a single character was no problem
def incrementChar(char):
if char == "z":
return "a"
else:
return chr(ord(char)+1)
and I tried writing a recursive function that would take the last character of the input increment that one all the way up to "z", take the second to last character of the input and increment it by one while letting the last character run from "a" to "z" again.
I tried this code
def incrementString(string):
possibilities = []
for i in range(len(string)-1, -1, -1):
if i == len(string)-1 and string[i] != "z":
while string[i] != "z":
possibilities.append(string.replace(string[i], incrementChar(string[i])))
string = string.replace(string[i], incrementChar(string[i]))
print(string)
else:
incrementString(string[i:])
return possibilities
and the first part works, it does run the last character to z and then increases the second to last by one, but starts increasing all characters at once and breaks in an infinite loop.
I'm sure that I'm overseeing something quite obvious, (the issue has to be that the second to last character is not being fixed while the last loops through the alphabet) maybe someone could help me with it...
Thanks!