Reverse


#1




This code worked and I got it from somewhere else. However, I don't understand how this worked. Can anyone explain this for me?Thanks.


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


#2

i would really recommend to write your own solution, this solution is rather complicated, and you will learn more from writing your own solution

if you want to understand this solution read about recursive functions, that should help


#3

Nothing needs to be done to reverse an empty string.

The reverse of all but the first character followed by the first character is going to result in the reverse of the whole string.


#4

Hi @tttto ,

A basic recursion - see if you can figure out how it works.

def to(n):
    if n == 0:
        # base case
        return "o"
    else:
        # recursive case
        return "t" + to(n - 1)

print to(4)

Output ...

tttto

#5

Hi, thanks so much for the reply. I still cannot get it, can you please explain more? Thanks.


#6

Thanks, I got it. :slight_smile:


#7

Thanks so much for the suggestion. I tried it again by myself.


#8

Hi @tttto ,

The example above was intended to demonstrate recursion. It is not the same as the reverse function, but as another example of recursion, it shares some of the same principles with it.

If someone make this call ...

print to(0)

... we have a base case where the to function simply returns "o", and that gets displayed.

This is the statement that produced that result ...

return "o"

However if someone make this call ...

print to(1)

... we have a recursive case in which the function needs to call itself with to(n - 1), which is equivalent to to(0), in order to complete its result.

This is the statement that gets executed ...

return "t" + to(n - 1)

As specified by that statement, we're going to concatenate the str, "t", with whatever the call to to(n - 1) returns. Since the value of n was 1, to(n - 1) is equivalent to to(0). That is the base case that gives us back "o". Concatenating "t" and "o" gives us "to", and that is what gets printed.

Multiple instances of the to function can be executing at the same time, each with its own variables with the same name. In the above example, we had two instances executing at the same time, each with its own variable, n.

Now, suppose we have the statement, with n equal to 4 ...

print to(4)

The call to(4) is made; it makes the call to(3) and waits for a result.
The call to(3) makes a call to(2) and waits for a result.
The call to(2) makes a call to(1) and waits for a result.
The call to(1) makes a call to(0) and waits for a result.

Now, we have reached the base case where n == 0.
With this condition, the to function can return the result "o" without the function's having to call itself again.

The call to(0) returns "o" to the call to(1).
The call to(1) concatenates "t" + "o" and returns "to" to the call to(2).
The call to(2) concatenates "t" + "to" and returns "tto" to the call to(3).
The call to(3) concatenates "t" + "tto" and returns "ttto" to the call to(4).
The call to(4) concatenates "t" + "ttto" and returns "tttto" which is printed.

If you would like some additional explanation, please let us know.


#9

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