Question

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 odd or even')) → True print(ispalindrome('is this it?')) → False

0 0
Add a comment Improve this question Transcribed image text
Answer #1

Code

# method to check given string is
# palindrome or not
def isPalindrome(string):
    # splitting string to words list
    string=string.split()
    # making string with all words without spaces
    string=''.join(string)
    # Getting length of the string
    length=len(string)
  
    # loop to check whether the give string is
    # palindrome or not
    # returning false if any character is not equal with
    # opposite index character
    for i in range(0,length):
        # if character not equal then return false
        if(string[i]!=string[length-i-1]):
            return False
    # return true if condition satisfies
    return True

print(isPalindrome("never odd or even"))
print(isPalindrome("is this it?"))

Screenshot:

output:

note

PLEASE GIVE ME UP VOTE

THANKYOU

Add a comment
Know the answer?
Add Answer to:
python Problem 3 (Palindrome) Write a function called ispalindrome(string) that takes a string (with spaces) as...
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
  • 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....

  • 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

  • Implement and test the following function://bool ispalindrome(string s)//return true iff s is a palindrome//For example: ispalindrome("RADAR")...

    Implement and test the following function://bool ispalindrome(string s)//return true iff s is a palindrome//For example: ispalindrome("RADAR") return true, //ispalindrome("ABCD") returns false. Enter word to find if the word is palindrome: RADAR The word is RADAR a palindrome Press any key to continue .. Enter word to find if the word is palindrome: ABCDEF The word is ABCDEF is not a palindrome Press any key to continue......

  • A palindrome is a word, phrase, or sequence that reads the same backward as forward, e.g.,...

    A palindrome is a word, phrase, or sequence that reads the same backward as forward, e.g., madam or nurses run. In this program, ask the user to input some text and print out whether or not that text is a palindrome. Create the Boolean method isPalindrome which determines if a String is a palindrome, which means it is the same forwards and backwards. It should return a boolean of whether or not it was a palindrome. Create the method reverse...

  • 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...

  • You need to write a program (one java class containing Main calling function isPalindrome (String str)....

    You need to write a program (one java class containing Main calling function isPalindrome (String str). The function isPalindrome (returns Boolean T/F) needs to determine whether or not a string is a palindrome, using recursion. The algorithm to check whether a string is a palindrome is shown below: /* check for first and last char of String: * if they are same then do the same thing for a substring * with first and last char removed. and carry on...

  • 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;...

  • Please write in java Write a main program that runs the following two recursive methods demonstrating...

    Please write in java Write a main program that runs the following two recursive methods demonstrating that they work. #1) Write a recursive method writeSquares that accepts an integer n and prints the first n squares with the odd squares in decending order, followed by the even squares in acending order. For example • writeSquares(8): prints . 49 25 9 1 4 16 36 64 • writeSquares(11); prints 121 81 49 25 9 1 2 16 36 64 100 •...

  • C Program: Write the following function that takes a user input string, computes, and determines whether the input is palindromic or not by returning either true or false:

    A palindrome is a word, phrase, number, or other sequence of symbols or elements that reads the same forward or reversed. Examples: “Dad”, “Anna”, “Never odd or even”, etc. For this problem, we will only consider a string to be palindromic if its combined alpha-numeric characters (‘A’-’Z’, ‘a’-’z’, and ‘0’-’9’) can be read the same both forward and backward, by skipping all other non-alphanumeric characters.Write the following function that takes a user input string, computes, and determines whether the input is palindromic or not...

  • Write a Python function that uses the stack data structure to check if a string is...

    Write a Python function that uses the stack data structure to check if a string is a palindrome or not. You can use any of the dollowing stack methods: push(), pop(), peek, empty(). Note that a palindrome is a word, phrase, or sequence that reads the same backwards as forward

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