0

I'm using the Python Tutorial visualize webpage to try and understand the flow of this function that reverses a string:

text = "hello"
def reverse(text):
    if len(text) <= 1:
        return text
    else:
        return reverse(text[1:]) + text[0]

print reverse(text)

I understand what this function does, but even with the visualizer, I'm still not quite grasping how the last line operates in the function in terms of the flow of the function as it loops through the characters in the string:

return reverse(text[1:]) + text[0]

I get that by itself reverse(text[1:]) returns o and that text[0] returns h

But again, not quite experienced enough to understand how the function is set up to loop through the string using [1:] and [0] — any explanation would be greatly appreciated in terms of how the final string retuned from the function is 'olleh'

8
  • reverse(text[1:]) (eventually) returns 'olle', not just 'o'! Think about the case len(text) == 2 and work out from there. Commented Jun 1, 2015 at 19:27
  • @jonrsharpe I understand what it eventually returns, I'm asking for a breakdown of how you get there using the splice methods at the end of the function. Commented Jun 1, 2015 at 19:29
  • Do you mean "slice"? It's not clear what exactly you don't understand. And please leave the "tags" out of the title. Commented Jun 1, 2015 at 19:30
  • @jonrsharpe What I don't understand is how the two built in slice functions on the last line of the reverse(text) function work together to arrive at the final reversed string. Commented Jun 1, 2015 at 19:32
  • Have you tried text[1:] on its own? Do you know what that does? Commented Jun 1, 2015 at 19:33

2 Answers 2

3
reverse("hello")
-> reverse("ello") + "h"
-> reverse("llo") + "e" + "h"
-> reverse("lo") + "l" + "e" + "h"
-> reverse("o") + "l" + "l" + "e" + "h"
-> "o" + "l" + "l" + "e" + "h"
-> "olleh"

This is to show the purpose of the return reverse(text[1:]) + text[0] line and its behaviour in action.

Sign up to request clarification or add additional context in comments.

8 Comments

Thanks for this breakdown, appreciate it. Just to be clear on what's happening here: is the text variable for text[0] basically storing each character it slices off as the recursive function calls keeps cycling through the string? or is it that with each recursive call another text[0] is being added?×Comments may only be edited for 5 minutes×Comments may only be edited for 5 minutes×Comments may only be edited for 5 minutes
The text variable refers to a different string in every call of the reverse function. The text[0] value is actually a single-character string taken from the beginning of the text string.
But it seems to be accumulating in your example, right? I think that's what I'm trying to understand. How/where are all the single characters text[0] is chopping off the string being stored to result in the final, reversed string? First is x = "h" then x = "e" + x = "h" etc, they're adding up, no?
The reverse function actually obtains two strings. One by calling itself recursively, another as text[0]. It simply concatenates them and returns the resulting string.
Yes, we can say that. The + operator is where bigger string is composed from smaller strings. A very useful approach to understand any recursive function is to consider that the function already knows how to do what it's expected to do. In our case reverse(text[1:]) simply returns reversed version of the provided string.
|
2

Here's the sequence of recursive calls, and what they return. You'll see that reverse("hello") returns the result of reverse("ello") plus "h".

reverse("hello")
  --> return reverse("ello") + "h"

So then the question is what does reverse("ello") return?

reverse("ello")
  --> return reverse("llo") + "e"

Continuing on...

reverse("llo")
  --> return reverse("lo") + "l"

reverse("lo")
  --> return reverse("o") + "l"

Finally, it bottoms out when reverse("o") is called. Here len(text) <= 1, and so it returns simply "o".

reverse("o")
  --> return "o"

You can then work your way back up from bottom to top to calculate the return value from the original reverse("hello") call.

5 Comments

Thanks a lot John for your very illustrative answer, appreciate it. Maybe I'm not understand the order of operations at the end of the function with return reverse(text[1:]) + text[0] why doesn't x = "llohe" after the first time through? Instead it looks like x = "ello" (and then the function works with that string on the next iteration through).
When text is "hello", text[1:] is "ello". For any string s, s[1:] is that same string with the first character chopped off.
But just so I'm clear, why doesn't the first iteration through the function return "elloh" instead of just "ello" — doesn't the + text[0] add the 'h' onto the end of the string?
If the last line were return text[1:] + text[0] then it would return "elloh". But it's not, it's return reverse(text[1:]) + text[0]. That's the tricky part, that recursive call to reverse(). To figure out the result of this expression, you have to pause things, figure out what reverse(text[1:]) evaluates to, then resume once you have that answer.
Thanks, your last comment really helped clear this up for me. So basically, by returning the rev(x) function with the added splice function inserted at the end of the rev(x) function, that's what makes it recursive?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.