Advertisement
Guest User

Untitled

a guest
Jul 23rd, 2016
71
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.87 KB | None | 0 0
  1. A semordnilap is a word or a phrase that spells a different word when backwards ("semordnilap" is a semordnilap of "palindromes"). Here are some examples:
  2.  
  3. nametag / gateman
  4. dog / god
  5. live / evil
  6. desserts / stressed
  7. Write a recursive program, semordnilap, that takes in two words and says if they are semordnilap.
  8.  
  9. This recursive function is not entirely straightforward. There are a few things that you need to check the first time you look at the inputs that you should not check on subsequent recursive calls: you need to make sure that the strings are not single characters, and also you need to be sure that the strings are not equal. If you do this check every time you call your function, though, this will end up interfering with the recursive base case (which we don't want!).
  10.  
  11. There's a few different ways you can perform checks on the inputs the first time. The first way would be to use keyword arguments. The second way would be to use a global variable, which you'll see in the next lecture video; however, using global variables is always a bit risky and thus not the best way to do this.
  12.  
  13. The third way to perform checks on the inputs the first time you see them, but not any subsequent time, is to use a wrapper function. This wrapper function performs some checks, then makes a call to the recursive function.
  14.  
  15. The idea of a wrapper function is really important. You'll see more wrapper functions later. To introduce you to the idea, we are providing you with the wrapper function; your job is to write the recursive function semordnilap that the wrapper function calls. Here is the wrapper function:
  16.  
  17. def semordnilapWrapper(str1, str2):
  18. # A single-length string cannot be semordnilap
  19. if len(str1) == 1 or len(str2) == 1:
  20. return False
  21.  
  22. # Equal strings cannot be semordnilap
  23. if str1 == str2:
  24. return False
  25.  
  26. return semordnilap(str1, str2)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement