Reversing a String (Recursive Solution)


Getting better with recursiveness. Using @datfatcat help with my last post, Is_Prime, I made a recursive solution to Reversing a string.

I can't really think of a way to make it recursive in just one function, so I used the Reverse function to test for the length of the inputed text, then had that call the helper_function to reverse and spit out the reversed string.

Please let me know if you can make any improvements!

def reverse(text):
    a = len(text)
    if a <= 1:
        return text
        counter = 0
        print a
        return helper_function(text, counter)

def helper_function(word,index_counter):
    if index_counter == len(word)-1:
        return word[index_counter]
        return helper_function(word, index_counter+1) + word[index_counter]
n = reverse("abcd")
print n

Is_Prime (solved with For Loop, spoilers inside)Is recursive possible?

I don't really understand why you would want to write two functions?
Why write and complicate it more than you need?
Here's my code, only need some exception handling in the function if one wants to.

def reverse(text):
    result = ""
    length = len(text)
    for i in text:
        result += text[length - 1]
        length -= 1
    return result


But...that's not recursive :frowning:
I'm pretty sure @rejor11 already figured out a solution using loops.


yes, i'm pretty sure, it was only a question of why.

he wants to make a loop himself instead of using the built in, but is it any different? why? is it faster somehow? should i start making recursive functions instead of using for or while?


No. Not for Python. Loops (iterative) are more pythonic. Recursion is expensive (eats a lot of memory)...I wouldn't say it's less efficient. I think there's also a limit to how many recursive calls you can make in Python but I'm not sure.
I'm pretty sure they did it as an exercise as recursion is better to use in some other languages.
But it all comes down to what you want to do (what problems you are trying to solve). Sometimes, recursion is easier to use to solve a problem than loops.


We do not cover recursion in the main track because it is a difficult concept to understand and learn, and does not fit into the picture of basic coding. In most cases recursion is slower than iteration, and should not be the go to method of choice.

Let's consider two examples, one iterative, one recursive. We'll use a factorial to demonstrate.

# iterative

def factorial(x):
    product = 1
    while x > 1:
        product *= x
        x -= 1
    return product
# recusive

def factorial(x):
    if x == 1: return            # Base case
    return x * factorial(x - 1)  # recursive case

Say we want to solve 20!. The iterative method will not use any more memory than if we are solving for 7!. The recursive method will use many times more memory, as @datfatcat described earlier.


Alright, i understand.
i thought loops were built kind of like a recursion, therefore i did not see the point. as i understand here, they are not. i do however think it's fun and self-educational tinkering around and doing same things in different ways anyway, this was educational guys, thanks :slight_smile:


Thanks for this info. I figured recursive algorithms in Python would use more memory and is probably slower as well. I do it as a way to challenge myself in a new way of thinking, and it's good practice.

That said I'm still very new to recursion.


This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.