Find an algorithm (subroutine) that when called repeatedly with the same character-string variable that is initialized with n characters, all different, will cycle through all the permutations of those n characters, so that for example, when called 24 times with a string of length 4, will have cycled that string through all 24 permutations and returned it to its initial state.
here's one subroutine in loose code
list perms(list[n])
{
state int i = 0
element e = list[ (i/(n-1)!) % n ]
list remainder = list[n] - e
if(remainder empty) {
i = i + 1
return e
}
else {
return e + perms(remainder)
}
}
ofcourse a subroutine with state is an example of imperative programming trash. try for example Haskell:
perms x = [ a:y | a <- x; y <- perms (x // [a]) ]
for something much neater