just as in our previous examplewe can use an empty string as the base case for the recursion the only possible arrangement of characters in an empty string is the empty string itself here is the completed recursive functiondef anagrams( )if =""return [selseans [for in anagrams( [ :])for pos in range(len( )+ )ans append( [:pos]+ [ ]+ [pos:]return ans notice in the else have used list to accumulate the final results in the nested for loopsthe outer loop iterates through each anagram of the tail of sand the inner loop goes through each position in the anagram and creates new string with the original first character inserted into that position the expression [:pos]+ [ ]+ [pos:looks bit trickybut it' not too hard to decipher taking [:posgives the portion of up to (but not includingposand [pos:yields everything from pos through the end sticking [ between these two effectively inserts it into at pos the inner loop goes up to len( )+ so that the new character can be added to the very end of the anagram here is our function in actionanagrams("abc"['abc''bac''bca''acb''cab''cba' didn' use "hellofor this example because that generates more anagrams than wanted to print the number of anagrams of word is the factorial of the length of the word examplefast exponentiation another good example of recursion is clever algorithm for raising values to an integer power the naive way to compute an for an integer is simply to multiply by itself times an * * * we can easily implement this using simple accumulator loop def looppower(an)ans for in range( )ans ans return ans divide and conquer suggests another way to perform this calculation suppose we want to calculate by the laws of exponentswe know that ( so if we first calculate we can just do one more multiply to get to compute we can use the fact that ( and