I am making an anagrams generator. The code is explained in the comments. I don't know why the resultArr
is always appending the same list. Sorry for my English and the inappropriate naming (should be resultList etc).
letterArrPermLen = len(letterArrPerm)
LastInd = letterArrPermLen
SecondLastInd = LastInd -1
resultArr = []
count=1
def Recursion(lastInd, secondLastInd):
global count
if lastInd == 1:
lastInd = letterArrPermLen
Further explained: # swap indices: # abcd acbd(swap1&2 of previous one (i.e.abcd)) acdb(swap2&3 of previous one (i.e.acbd)) # abdc(swap2&3 of the "abcd" above) adbc(swap1&2) adcb(swap2&3)
lastInd -= 1
secondLastInd = lastInd - 1
# swapping elements with indices lastInd & secondLastInd
letterArrPerm[lastInd], letterArrPerm[secondLastInd] = letterArrPerm[secondLastInd], letterArrPerm[lastInd]
# append the swapped list to the result List
copy = letterArrPerm
resultArr.append(copy)
count+=1
# set count to 100 otherwise the recursion goes forever
return Recursion(lastInd, secondLastInd) if count <= 100 else None
Recursion(LastInd, SecondLastInd)
print("result: ")
print(resultArr)
Does anyone know how to fix this code directly? I mean to modify my code to make it work.