Hi, the following project is the first small step in an anagram solver
that I am coding. I have four letters "a", "b", "c", "d" and the aim of the following code is to produce all possible permutations of these
letters, given that the first letter is always "a". (I have used two functions to systematically switch round the letters of the preceding list)
Put simply, I want to return the following output:
a b c d ("print LETTERS")
a c b d (first "print iterations") - produced by switching letters 2 and 3
a c d b (the next four lists are all from "print iterations" within the
a d c b while loop, the first of these four is produced by switching
a d b c letters 3 and 4 from the preceding list, and the next by
a b d c switching letters 2 and 3, and so on.
The program should then end with "Close" since the next list produced
by my code is "a", "b", "c", "d" - in other words the variable iterations
is now the same as the variable LETTERS - which should cause the while loop to stop. Unfortunately it doesn't, and I end up with an infinite loop.
Can anyone tell me why?
LETTERS = ["a", "b", "c", "d"] print LETTERS ### FUNCTION: Main def main(): letters = ["a", "b", "c", "d"] iterations = replace_one_two(letters) print iterations ### Continue switching letters until we arrive back at the start ### i.e. when iterations = LETTTERS while iterations != LETTERS: iterations = replace_three_four(iterations) print iterations iterations = replace_one_two(iterations) print iterations print "Close" ### FUNCTION: Switch letters in 2nd and 3rd position def replace_one_two(name): left = name right = name name.remove(left) name.remove(right) name.insert(1, right) name.insert(2, left) return name ### FUNCTION: Swith letters in 3rd and 4th position def replace_three_four(name): left = name right = name name.remove(left) name.remove(right) name.insert(2, right) name.insert(3, left) return name main()