Ask Question

A palindrome is a word or sequence of characters which reads the same backward and forward, such as madam, dad, racecar, 5885. Write a class "Palindrome" that takes a word as input and returns if the word is palindrome or not; implement a driver program that asks users to enter 5 words; and prints the list of palindrome and not palindrome words. If user input is: dad, 123, mom, xyz, racecar The output will be: Palindrome: dad, mom, racecar Not palindrome; 123, xyz

+1
Answers (1)
  1. 4 May, 01:28
    0
    class Palindrome: def __init__ (self, word) : self. word = word self. status = self. checkPalindrome (word) def checkPalindrome (self, word) : for i in range (0, len (word) / /2) : if (word[i]! = word[len (word) - 1 - i]) : return False return True words = input ("Input five words: ") word_list = words. split (" ") p_list = [] np_list = [] for w in word_list: p = Palindrome (w) if (p. status) : p_list. append (p. word) else: np_list. append (p. word) print ("Palindrome: " + " ". join (p_list)) print ("Not palindrome: " + " ". join (np_list))

    Explanation:

    Firstly create a Palindrome class with one constructor that will take one input word (Line 1-2). There is a checkPalindrome method in the class to check if the input word is a palindrome (Line 6-10). If so, return True and set the boolean value to status instance variable (Line 4) or False it is not a palindrome.

    In the main program, we ask user to input five words and split them into a list of individual words (Line 12 - 13). Use a for loop to traverse through each word from the word_list and create a Palindrome object (Line 17 - 19). When a Palindrome object is created, it will automatically call the checkPalindrome method and set the status either true or false. If the object status is true, add the word of current Palindrome object to p_list otherwise add it to np_list (Line 19-22).

    Lastly, display the list of palindrome and non-palindrome (Line 24-25).
Know the Answer?
Not Sure About the Answer?
Find an answer to your question 👍 “A palindrome is a word or sequence of characters which reads the same backward and forward, such as madam, dad, racecar, 5885. Write a ...” in 📗 Computers & Technology if the answers seem to be not correct or there’s no answer. Try a smart search to find answers to similar questions.
Search for Other Answers