Recursion function problem

This is the place for queries that don't fit in any of the other categories.

Recursion function problem

Postby Blanqui » Sat Nov 16, 2013 1:16 pm

Hello,

I am a beginner in python, and I am trying to do the following exercise:

"Write a function that takes a string as a parameter and returns True if the string is a palindrome " (recursively)

Here's what I've written:

Code: Select all
def palindrom(str):
    pal=""
    while len(pal) != len(str):
          pal = str[-1] + reverse(str[:-1])


This gives me the string backwards. However, when I am trying to compare them, I keep getting errors. I have tried using == and is, but they don't seem to work. Why can't i use these boolean operators in a recursive function and how can I solve it?
Last edited by stranac on Sat Nov 16, 2013 1:18 pm, edited 1 time in total.
Reason: Added code tags. Locked.
Blanqui
 
Posts: 4
Joined: Sat Nov 16, 2013 12:17 pm

Re: Recursion function problem

Postby stranac » Sat Nov 16, 2013 1:20 pm

What's your full code?
What errors are you getting?
Friendship is magic!

R.I.P. Tracy M. You will be missed.
User avatar
stranac
 
Posts: 1209
Joined: Thu Feb 07, 2013 3:42 pm

Re: Recursion function problem

Postby Blanqui » Sat Nov 16, 2013 1:28 pm

Full code:

Code: Select all
def reverse(str):
    pal=""
    while len(pal) != len(str):
        pal = str[-1] + reverse(str[:-1])
    if pal == str:
        return True
    else:
        return False
reverse("test")


Error message:

TypeError: Can't convert 'bool' object to str implicitly
Last edited by Mekire on Sun Nov 17, 2013 4:43 am, edited 3 times in total.
Reason: lock.
Blanqui
 
Posts: 4
Joined: Sat Nov 16, 2013 12:17 pm

Re: Recursion function problem

Postby stranac » Sat Nov 16, 2013 2:17 pm

Post the entire error traceback.
Also, the reverse() function seems to be missing, as well as the part where you're calling this function.
Friendship is magic!

R.I.P. Tracy M. You will be missed.
User avatar
stranac
 
Posts: 1209
Joined: Thu Feb 07, 2013 3:42 pm

Re: Recursion function problem

Postby Blanqui » Sat Nov 16, 2013 3:16 pm

I'm sorry, I have edited my previous post to the correct code.

Full error:


Traceback (most recent call last):
File "C:\Users\xx\Desktop\python\reverse recurse.py", line 9, in <module>
print(reverse("h"))
File "C:\Users\xx\Desktop\python\reverse recurse.py", line 4, in reverse
palindrom = str[-1] + reverse(str[:-1])
TypeError: Can't convert 'bool' object to str implicitly
Blanqui
 
Posts: 4
Joined: Sat Nov 16, 2013 12:17 pm

Re: Recursion function problem

Postby stranac » Sat Nov 16, 2013 3:22 pm

So it's actually a reverse() function?

Your function returns True or False.
You're trying to add this returned value to a string. You can't do that.

I would suggest a fix, but your function makes no sense.
If it's a function to reverse a string, why are you checking for palindromes in it?
If it's a palindrome checking function, why does it do reversing?
Friendship is magic!

R.I.P. Tracy M. You will be missed.
User avatar
stranac
 
Posts: 1209
Joined: Thu Feb 07, 2013 3:42 pm

Re: Recursion function problem

Postby Somelauw » Sat Nov 16, 2013 4:13 pm

Blanqui wrote:Full code:

Code: Select all
def reverse(str):
    pal=""
    while len(pal) != len(str):
        pal = str[-1] + reverse(str[:-1])
    if pal == str:
        return True
    else:
        return False
reverse("test")


Error message:

TypeError: Can't convert 'bool' object to str implicitly


You seem to be doing two things at once. You should instead write two separate functions.
One function that reverses a string. Another function that uses the reverse function to check if a string is a palindrome.
Join the #python-forum IRC channel on irc.freenode.net!
Somelauw
 
Posts: 73
Joined: Tue Feb 12, 2013 8:30 pm

Re: Recursion function problem

Postby Crimson King » Sat Nov 16, 2013 4:30 pm

I don't think that's the point of the exercise, besides there's no need to reverse the string at all.

Maybe the problem lies with his understanding of recursion.

Blanqui, try to write in pseudocode what you think the recursive function should do.
User avatar
Crimson King
 
Posts: 131
Joined: Fri Mar 08, 2013 2:42 pm
Location: Buenos Aires, Argentina

Re: Recursion function problem

Postby Blanqui » Sat Nov 16, 2013 4:34 pm

Thanks for your feedback. I rewrote the code now, and it seems to work well:

Code: Select all
def palin(str):
    if len(str)<=1:
        return True
    elif str[0] != str[-1]:
        return False
    return palin(str[1:-1])
   
print(palin("kayak"))

>>>
True
>>>



Crimson King wrote:I don't think that's the point of the exercise, besides there's no need to reverse the string at all.

Maybe the problem lies with his understanding of recursion.

Blanqui, try to write in pseudocode what you think the recursive function should do.


Yes, it's still pretty hard to grasp it fully. I thought the first code would repeat itself to the recursive call, and skip "while loop" on the last call. Then do the comparison of str and palindrom.
Last edited by Mekire on Sun Nov 17, 2013 4:42 am, edited 1 time in total.
Reason: lock.
Blanqui
 
Posts: 4
Joined: Sat Nov 16, 2013 12:17 pm

Re: Recursion function problem

Postby Mekire » Sun Nov 17, 2013 4:42 am

A few comments now that you have found your solution. You should never use the names of built in functions as variables. In this case you shouldn't use str. Next, you need to understand that this assignment is merely an exercise to introduce you to recursion. In reality, one would never do it this way.

Here is a much simpler non-recursive solution:
Code: Select all
def is_palindrome(word):
    return word == word[::-1]

Recursion is a tool that has its place, but sometimes it is simply unnecessary.

-Mek
User avatar
Mekire
 
Posts: 1009
Joined: Thu Feb 07, 2013 11:33 pm
Location: Amakusa, Japan


Return to General Coding Help

Who is online

Users browsing this forum: Bing [Bot], snippsat, W3C [Linkcheck] and 3 guests