Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Recursion method on finding whether a given string is a palindrome or not

def palindrome(w):
    if len(w)==0 or len(w)==1:
        p = 'yes'
        return p
    else:
        if w[0]==w[-1]:
            del w[0]
            del w[-1]
            palindrome(w)
        else:
            p = 'no'
            return p           
s=input()
w=list(s.replace(" ",""))
print(palindrome(w))

when input is not a palindrome, the code works fine. but when the input is a palindrome. it is returning the value none instead of yes. why is that?

ps:- I’m a beginner-level programmer. I’m still learning. so please be kind enough to explain me more straightforwardly.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

You will have to propagate the result of your palindrome(w) call:

return palindrome(w)

If a function ends without a return being executed, the function will automatically return the value None. Thus, e.g. if you call palindrome(["a", "b", "a"]), you will evaluate the subcall palindrome(["b"]), then discard its result, and return None for the value of palindrome(["a", "b", "a"]). If you use return palindrome(w), then the value of palindrome(["a", "b", "a"]) will become the value of palindrome(["b"]), which is the desired logic.


The following are not errors, just good practices.

It is probably better to make the function more general. Instead of making a palindrome function that can only handle lists, why not make it so it can handle any sequence, including strings directly? However:

del w[0]
del w[-1]

This does not work on strings, and will give you a TypeError: ‘str’ object doesn’t support item deletion. You cannot use del to remove characters in a string: Python strings are immutable. You have to create a new string without the first and last character instead:

w = w[1:-1]

Now the function will work both for palindrome([1, 2, 3]) and palindrome("abc").

Finally, it would be better to use the standard Boolean type for "yes" and "no". If you need to display yes or no to the user, implement this logic outside the function that performs the test, preferably at the very last moment before you need to display it.


The final form:

def palindrome(w):
    if len(w)==0 or len(w)==1:
        return True
    else:
        if w[0]==w[-1]:
            w = w[1:-1]
            return palindrome(w)
        else:
            return False

s = input()
w = s.replace(" ","")
if palindrome(w):
    print("yes")
else:
    print("no")
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading