2

I am not sure I even fully understand the second half of this question. I will post the question I'm stuck on, and the code I have so far. At the moment Module 2 is the same as Module 1, as a place holder so i could test my menu function.

Please forgive my formatting, this is my first post.

Question: From the knowledge learned throughout the course write a menu-driven palindrome check using two functions. One function will compare the first letter to the last letter working its way to the center. The second function will break the work in two. The first half will be compared to the mirror image of the second half to determine if the word is a palindrome. Please note the functions must work for even and odd number of letter strings. Your submission must be able to compare the words deed as well as Dad.

My Code so far:

Module 1 Recursive Algorithm

def isPalindrome(string) :

   if len(string) <= 1 :

      return True

   if string[0] == string[len(string) - 1] :

      return isPalindrome(string[1:len(string) - 1])

   else :

      return False

Module 2 divide word method

def isPalindrome2(string) :

   if len(string) <= 1 :

      return True

   if string[0] == string[len(string) - 1] :

      return isPalindrome2(string[1:len(string) - 1])

   else :

      return False 

print( "Please think of a word to test and select a method")

print("1) Using compare letters method")

print("2) Using dividing word in half method and comparing")

print("3) Quit")


choice = input("Select option: #")

if choice == '1':

    st = input("Enter word you wish to test: ")

    if (isPalindrome(st)) :

        print ("Option #1, The word: "+ st +", is a palendrome")

    else : 
        print ("Option #1, The word: "+ st +", is NOT a palendrome")

elif choice =='2':

    st = input("Enter word you wish to test: ")

    if (isPalindrome2(st)) :

        print ("Option #2, The word: "+ st +", is a palendrome")

    else : 

        print ("Option #2, The word: "+ st +", is NOT a palendrome")

elif choice == '3':


    print("You quit.")
    exit() 
Prateek Dewan
  • 1,587
  • 3
  • 16
  • 29

1 Answers1

0

The "divide word" method is the recognition that, if you split a palindrome in half and reverse the order of the second half, you should get the same word twice. For example, pretend for a moment that "feeddeef" was a word. If I split it down the middle I get

feed
deef

If I then reverse the letters in the second word, I get

feed
feed

Since the two strings match, "feeddeef" is a palindrome. Of course, there's a slight alteration to this process for words of odd length because you need to know what to do with the middle letter.

Daniel Walker
  • 6,380
  • 5
  • 22
  • 45