Question

Write a function check palindrome, which takes a string x as argument and which returns True...

Write a function check palindrome, which takes a string x as argument and which returns True if x is a palindrome,
and False otherwise. A palindrome is a word that reads the same backwards as forwards (like for example
“racecar”). Your function should be recursive (i.e. call itself) and proceed as follows.


1. If x is a string of length 0 or 1 return True.
2. If the rst and the last letter of x are di erent, return False.
3. Otherwise, remove the rst and last letter of x and use the function check palindrome to check whether this
shorter word is a palindrome. Return that result.

0 0
Add a comment Improve this question Transcribed image text
Answer #1
def is_palindrome(s):
    if len(s) <= 1:
        return True
    else:
        return s[0] == s[-1] and is_palindrome(s[1:-1])


print(is_palindrome("abba"))
print(is_palindrome("hannah"))
print(is_palindrome("abc cba"))
print(is_palindrome("radar"))

print(is_palindrome("notpalindrome"))
print(is_palindrome("hello"))

True True True True False False

Add a comment
Know the answer?
Add Answer to:
Write a function check palindrome, which takes a string x as argument and which returns True...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • python Problem 3 (Palindrome) Write a function called ispalindrome(string) that takes a string (with spaces) as...

    python Problem 3 (Palindrome) Write a function called ispalindrome(string) that takes a string (with spaces) as argument and returns True if that string (ignoring spaces) is a palindrome. A palindrome is a word or phrase that spells the same thing forwards as backwards (ignoring spaces). Your program should use a recursive process to determine the result, by comparing the first and last letters of the string, and if necessary, calling ispalindrome() on the rest of the string. Sample run: print(ispalindrome('never...

  • Python Question: A palindrome is a string that reads identical forwards and backwards. Examples include abba,...

    Python Question: A palindrome is a string that reads identical forwards and backwards. Examples include abba, abcba, a1b1b1a, houstonnotsuoh etc. Your task is to write a function ispalindrome(string) that reads an input string and returns True if both of the following are true: The string is a palindrome The string has at least one letter (uppercase or lowercase) For example: ispalindrome('racecar') should return True PS: ispalindrome('123321') should return False ispalindrome('racecar') should return True

  • Fix the function so that it checks if the string is a palindrome #include <iostream> using...

    Fix the function so that it checks if the string is a palindrome #include <iostream> using namespace std; //Fix the function so that it checks if the string is a palindrome //(same forwards as backwards ex: racecar / radar) bool is_palindrome(string str, int i){ //base cases if (/* add the condition */) return false;    if (/* add the condition */) return true;    //recursive call return is_palindrome(str, i-1); } int main(){ string x; cin >> x;    if (is_palindrome(x,x.length())){...

  • write a loop that processes a string and returns true if the string is a palindrome...

    write a loop that processes a string and returns true if the string is a palindrome and false if it is not. A String is a palindrome if it is spelled the same forward as it is backward. For example, the words “joe”, “ age”, “pokemon”, “b”, “kayak” and “racecar” are all palindromes. C++ I wrote a sample but not compiling... #include"stdafx.h" #include<iostream> #include<string> int main() { Int x = 0; Int y= 0; string word = “racecar” x= 0;...

  • A palindrome is a word, phrase, number, or other sequence of characters which reads the same...

    A palindrome is a word, phrase, number, or other sequence of characters which reads the same backward as forward, such as madam or racecar. Write a recursive method in java that accepts a integer as its argument and returns true if it is palindrome, false otherwise. public class Recursion {                 public static void main(String[] args) {                                 Recursion r = new Recursion();                                 System.out.println(“Is 12321 a palindrome? “+ra.isPalindrome(12321)); //true                 }                 public Boolean isPalindrome(int num){                                 return false;...

  • Implement a function that returns true if a String is Palindrome or false otherwise. A Palindrome...

    Implement a function that returns true if a String is Palindrome or false otherwise. A Palindrome is a String that reads from right and left the same. An empty String, and a String with one character are Palindrome. public static boolean isPalindrome (String str) {

  • Write a function palcheck (char word II) which takes a C-style string word (or a C++...

    Write a function palcheck (char word II) which takes a C-style string word (or a C++ string word if you wish) and returns or false depending on whether or not the string is a palindrome. (A palindrome is a string which reads the same backwards and forwards. Some classic example of palindromes are radar, racecar, toot, deed, bib, civic, redder and madam.) Use your function from part (a) in a complete C++ program which, for each small letter 'a' through...

  • palindrome is a string that reads the same both forward and backward. C++ For example, the...

    palindrome is a string that reads the same both forward and backward. C++ For example, the string "madam" is a palindrome. Write a program that uses a recursive function to check whether a string is a palindrome. Your program must contain a value-returning recursive function that returns true if the string is a palindrome and false otherwise. Do not use any global variables; use the appropriate parameters.

  • 1.) Write a recursive function named isPalindrome that will take a single string as an argument...

    1.) Write a recursive function named isPalindrome that will take a single string as an argument and determine if the argument is a palindrome. The function must return True if the argument is a palindrome, False otherwise. Recall that any string is a palindrome if its first and last characters are the same and the rest of it is also a palindrome (therefore, a single character is a palindrome): 2.) Consider a text file named samplestrings.txt that contains a collection...

  • A palindrome is a word that is spelled the same forward and backward. For example, rotor...

    A palindrome is a word that is spelled the same forward and backward. For example, rotor and redder are palindromes, but motor is not. Write a recursive function that takes a word (string) and check if it is palindrome or not (The function should return True if the word is palindrome otherwise it should return False). USING PYTHON

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT