verview You will implement a program to identify palindrones. Recall that a pali
ID: 3726948 • Letter: V
Question
verview You will implement a program to identify palindrones. Recall that a palindrome is a phrase that -ignoring casing and non-letter characters is spelled the same for wards as backwards. An example of a palindrome is the title of this lab. You will also implement a function to construct a palindrone. Functions You will w rite the following functions 1. def print_menu: prints the main menu to the screen (a) Function behavior i. Prints a menu with three options: "Check a palindrome; "Make a palindrome"; and "Quit" (b) Restrictions i. Do not use input to read a me choice from the user 2. def get menu choiceO (a) Function behavior: i. Read the user's selected menu option using input), and validate it ii. The validated selected option is returned (b) Restrictions i. No other functions are called from getmenu choice 3. def get-phrase (a) Function behavior: i. Ask the user to input an English phrase as a string. ii. Validate that at least one character is entered ii. Return the validated phrase. (b) Restrictions i. No other functions are called from get.phrase I. def is palindrome (phrase): given a string named phrase, this function returns True if the string is a palindrome, and False otherwise. (a) Function behavior: i. Given phrase, you must determine if the string is identical forwards and backwards, if we gnore upper/lowercase and any non-letter characters like spaces and punctuation ii. Adapt the loop from lecture, which assumes that the entered string is lower-case and does not contain non-letter characters. From now on, ealidate always implies a loop that repeatedly asks r input until the input i valid.Explanation / Answer
Python3.6 Code:
##python 3.6
import sys
def print_menu() :
print("Menu")
print("1. Check a aplindrome",)
print("2. Make a palindrome",)
print("3. quit")
def get_menu_choice():
choice = input("Enter the option(1/2/3) : ")
if choice in ["1" , "2", "3"]:
return int(choice)
else :
print("Wrong Option")
return None
def get_phrase():
string = input("Enter a phrase : ")
if len(string) == 0:
print("Invalid String Entered")
return None
else :
return string
def is_palindrome(phrase):
i = 0
j = len(phrase) -1
lower_phrase = phrase.lower()
while( i < j):
while(i < j):
if lower_phrase[i].isalpha():
break
else :
i += 1
while(i < j):
if lower_phrase[j].isalpha():
break
else :
j -= 1
if lower_phrase[i] != lower_phrase[j] :
return False
i += 1
j -= 1
##end of while loop.
return True
def menu_check_palindrome():
phrase = get_phrase()
if phrase == None:
pass
else :
if is_palindrome(phrase):
print(phrase, "is a palindrome")
else:
print(phrase, "is not a palindrome")
def make_palindrome(phrase, skip_last):
if skip_last :
i = len(phrase) - 2
else :
i = len(phrase) -1
while i >= 0:
##Removing white spaces.
while i >= 0:
if phrase[i].isalpha():
break
else:
i -= 1
phrase = phrase + phrase[i]
i -= 1
return phrase
def get_repeat_last():
choice = input("Do you want last letter of the phrase(y/n):")
if choice.lower() == 'y':
return True
elif choice.lower() == 'n':
return False
else :
print("Invalid choice")
return None
def menu_make_palindrome():
phrase = get_phrase()
if phrase == None:
return
skip_last = get_repeat_last()
if skip_last == None:
return
print(make_palindrome(phrase, skip_last))
def main():
while 1:
print_menu()
choice = get_menu_choice()
if choice == 1:
menu_check_palindrome()
elif choice == 2:
menu_make_palindrome()
elif choice == 3:
break
else:
pass
if __name__ == "__main__" :
main()
Output after runing this program:
-bash-4.2$ python3.6 main.py
Menu
1. Check a aplindrome
2. Make a palindrome
3. quit
Enter the option(1/2/3) : 1
Enter a phrase : rohit
rohit is not a palindrome
Menu
1. Check a aplindrome
2. Make a palindrome
3. quit
Enter the option(1/2/3) : 1
Enter a phrase : r qr
r qr is a palindrome
Menu
1. Check a aplindrome
2. Make a palindrome
3. quit
Enter the option(1/2/3) : 2
Enter a phrase : rohit
Do you want last letter of the phrase(y/n):n
rohittihor
Menu
1. Check a aplindrome
2. Make a palindrome
3. quit
Enter the option(1/2/3) : 2
Enter a phrase : rohit j
Do you want last letter of the phrase(y/n):y
rohit jtihor
Menu
1. Check a aplindrome
2. Make a palindrome
3. quit
Enter the option(1/2/3) : 3
Please share your feedback and concern.
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.