Question

In: Computer Science

6. Write a Python function that checks whether a passed string is palindrome or not. Note:-A...

6. Write a Python function that checks whether a passed string is palindrome or

not. Note:-A palindrome is a word, phrase, or sequence that reads the same backward

as forward

. Some examples you may try: “madam”

redder

“race car”

Eva, Can I Stab Bats In A Cave?

If the argument passed is not a string,

invoke an exception or an assertion

and state in a comment which one you have chosen and why.

Solutions

Expert Solution

Step-1) check if the passed input is of type str or not .If input is not of type str raise TypeError showing input must be of type str.

we should raise the exception to make user aware of his wrong input as early as possible.It is always better to detect error as early as possible in programming to avoid useless computations.

step-2)form a string of alphabets to avoid special characters and spaces in the text.

Step-3)If every ith character is same from beginning and end of the string , than we can say that the string is a palindrome.

Code:

def check_palindrome(string):
    # raise a TypeError if the passed input is not of str type
    if not isinstance(string, str):
        raise TypeError("Input must be of type String")
    else:
        # first form a string of alphabets from a given input string
        final_string = ""
        for char in string:
            if char.isalpha():  # returns true if the character is an alphabet
                final_string += char.upper()  # converts the alphabet to uppercase

        n = len(final_string)

        # final_string[i] gives the ith character from start and final_string[n-i-1] gives the ith string from end
        for i in range(0, n // 2):
            if final_string[i] == final_string[n - i - 1]:
                continue
            else:
                return "given input is not a palindrome"
        return "given input is a palindrome"


if __name__ == '__main__':
    # main method for testing out the above method
    print(check_palindrome("race car"))

If you have any doubt in understanding the solution or want any modification in code , just put a comment on the answer and i would be happy to help you out.


Related Solutions

Write a function that takes a string as an argument checks whether it is a palindrome....
Write a function that takes a string as an argument checks whether it is a palindrome. A palindrome is a word that is the same spelt forwards or backwards. Use similar naming style e.g. name_pal. E.g. If we call the function as abc_pal(‘jason’) we should get FALSE and if we call it a abc_pal(‘pop’) we should get TRUE. Hint: define your function as abc_pal(str). This indicates that string will be passed. Next create two empty lists L1=[] and L2=[] ....
Python please Write a function that takes a string as an argument checks whether it is...
Python please Write a function that takes a string as an argument checks whether it is a palindrome. A palindrome is a word that is the same spelt forwards or backwards. Use similar naming style e.g. name_pal. E.g. If we call the function as abc_pal(‘jason’) we should get FALSE and if we call it a abc_pal(‘pop’) we should get TRUE. Hint: define your function as abc_pal(str). This indicates that string will be passed. Next create two empty lists L1=[] and...
Write an X86-series assembly language program that checks whether input string is palindrome or not. A...
Write an X86-series assembly language program that checks whether input string is palindrome or not. A palindrome is a word, number, phrase or any other sequence which reads the same backward as forward e.g. madam, racecar. Sample Execution: Please enter a String: redivider The string is a palindrome Another Sample Execution: Please enter a String: abracadabra The string is not a palindrome
Write a program that determines whether an input string is a palindrome; that is, whether it...
Write a program that determines whether an input string is a palindrome; that is, whether it can be read the same way forward and backward. At each point, you can read only one character of the input string; do not use an array to first store this string and then analyze it (except, possibly, in a stack implementation). Consider using multiple stacks. In Pseudocode please
Write a function which takes two words as string arguments and checks whether they are anagrams.
Write a function which takes two words as string arguments and checks whether they are anagrams.
Write a function which takes two words as string arguments and checks whether they are anagrams...
Write a function which takes two words as string arguments and checks whether they are anagrams NB: Please use spider in anaconda(python) and include a picture of the code
Write a recursive a c++ code that checks if a number is Palindrome. A palindrome number...
Write a recursive a c++ code that checks if a number is Palindrome. A palindrome number is a number that reads the same from beginning to end and from end to beginning, in other words, a palindrome number remains the same when its digits are reversed. For example, 13431 is a palindrome number. 2332 is another one. (Note: Your algorithm should define and work with an integer number) The functionality of your code should be commented to explain what you...
Write a recursive a Java code that checks if a number is Palindrome. A palindrome number...
Write a recursive a Java code that checks if a number is Palindrome. A palindrome number is a number that reads the same from beginning to end and from end to beginning, in other words, a palindrome number remains the same when its digits are reversed. For example, 13431 is a palindrome number. 2332 is another one. (Note: Your algorithm should define and work with an integer number) The functionality of your code should be commented to explain what you...
Write a short recursive C++ function that determines if a string s is a palindrome, that...
Write a short recursive C++ function that determines if a string s is a palindrome, that is, it is equal to its reverse. For example,"racecar" and "gohangasalamiimalasagnahog" are palindromes. Please include the pseudo code so that I can understand better with simple English as much as possible.
C# Palindrome Permutation: Given a string, write a function to check if it is a permutation...
C# Palindrome Permutation: Given a string, write a function to check if it is a permutation of a palindrome. A palindrome is a word or phrase that is the same forwards and backwards. A permutation is a rearrangement of letters. The palindrome does not need to be limited to just dictionary words. Input: Tact Coa Output: True (permutations: "taco cat", "atco cta", etc.) Comment your code to explain it.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT