Explanation For Reverse_string Recursive Function
Solution 1:
The concept of recursion is that you call a the same function until the base case is encountered. In your case, the base case is when len(string) == 0
, where you return an empty string to the caller, which was a previous version of the same function
reverse_strings
, but with different parameters (usually a more "complex" function).
Suppose you have this simple string hi
, your function would behave like this:
Check if the base case is reached, if yes, return an empty string, otherwise go to next statement. Since we don't have an empty string, we go to the next statement.
Next statement calls the same function
reverse_strings
, but with different parameters than when you call it the first time to run it; in fact, the first time you call it, you do something like thisreverse_strings('hi')
. In theelse
, you are calling the function with a smaller version of your stringhi
, observe in the following statement:return reverse_strings(string[1:]) + string[0]
thatstring[1:]
is justi
. Now, basically you havereturn reverse_strings('i') + string[0]
. Note thatstring[0]
isH
. Here,reverse_strings('i')
, as I said above, you are calling your function again, but with a smaller version of your stringi
.Now, we are inside another function call. The first statement
if len(string) == 0: return ''
is evaluated. Is it true? No, becauselen(string)
is still different from0
. So we pass to the next statementelse: return reverse_strings(string[1:]) + string[0]
. Now, note that we are going to call the same function again. but with a smaller string, in this case, an empty string, sincestring[1:]
ofi
is an empty string. So our call can be summarised like thisreturn reverse_strings('') + i
.We are now in another "simplified version" of
reverse_strings
. The first statement is evaluatedif len(string) == 0: return ''
. Is it true? Yes, because, remember, our string is now an empty string. What happens now is that you return an empty string to the caller, which is the function in the point 3.Now, we have just returned an empty string to the caller at point 3, so we have
return '' + i
.'' + i
is nothing else than simplyi
, which you are going to return to the caller of this function at pointer 3, which is the function at point 2.Now, you have just returned
i
to the caller, specifically you have just returnedi
to this statementreturn reverse_strings('i') + string[0]
, wherestring[0]
isH
andreverse_strings('i')
is justi
that you have just returned. So, now you haveiH
, and you have just reversed the string.
Solution 2:
It uses slicing to concatenate the first letter onto the end, then pass the 2nd to remaining letters into the recursive function again.
Solution 3:
string[1:]
on the first call is ello
string[0]
is h
:
2nd recursive call string[1:] -> llo
string[0] -> e
3rd string[1:] ->
lo string[0] -> l
4th string[1:] ->
o string[0] -> l
5th string[1:] ->
"" string[0] -> o
So reverse_strings(string[1:])
calls the function recursively moving across the string and string[0]
concatenates each letter starting from the end which is what is returned at the end.
The graph below may help:
Solution 4:
To really understand this, I would express it in a declarative, sort-of logical form. Here's your code:
1: iflen(string) == 0: return''2: else: return reverse_strings(string[1:]) + string[0]
I'll call the first element of a string (string[0]
) its head and the remainder (string[1:]
) its tail. If a string is only 1 character long, we consider its tail to be the empty string. In terms of that vocabulary, here are the rules for what it means to reverse a string:
- The empty string reversed is the empty string.
- Any other string reversed is the reversed version of its tail, followed by its head.
In the case of, for example, abcd
, we apply rule 2 since rule 1 doesn't apply:
abcd
reversed isbcd
reversed, followed bya
.
Ok, what's bcd
reversed? Well, we can apply the same rule:
bcd
reversed iscd
reversed, followed byb
.
and down the chain:
cd
reversed isd
reversed, followed byc
,d
reversed is''
reversed, followed byd
,''
reversed is''
, by rule 1, because it is the empty string.
Going back up this list, you get:
''
followed byd
followed byc
followed byb
followed bya
which is dcba
, which is what we wanted!
Wrapping your head around recursion isn't easy. Try to solve some other problems with it or do some exercises that require it; this kind of practice really helps understanding.
Post a Comment for "Explanation For Reverse_string Recursive Function"