reverse_string 递归函数说明 [英] Explanation for reverse_string recursive function

查看:52
本文介绍了reverse_string 递归函数说明的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

无论我用 Python 可视化器运行多少次,我都无法弄清楚这段代码是如何工作的;有人可以告诉我以下代码的递归是如何工作的吗?

No matter how many times I run it by Python visualizer, I can't figure out how this code works; can someone PLEASE tell me how the recursion of this following code works?

def reverse_strings(string):
    if len(string) == 0: return ''
    else: return reverse_strings(string[1:]) + string[0]

reverse_strings('hello')

我自己写的,它有效,但我不知道它是如何工作的.我知道返回递归的工作原理是将ello"运行到递归函数中,但我一生都无法理解它是如何向后打印内容的.

I wrote it myself, and it works, but I have no idea how it works. I know return recursion works by running the "ello" into the recursive function, but I can't for the life of me understand how it's printing things backwards.

推荐答案

递归的概念是你调用相同的函数直到遇到基本情况.在您的情况下,基本情况是 len(string) == 0,您将空字符串 返回给调用者,这是同一函数的先前版本reverse_strings,但具有不同的参数(通常是更复杂"的函数).

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).

假设你有这个简单的字符串hi,你的函数会像这样:

Suppose you have this simple string hi, your function would behave like this:

  1. 检查是否达到基本情况,如果达到,则返回一个空字符串,否则转到下一条语句.由于我们没有空字符串,我们转到下一个语句.

  1. 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.

下一个语句调用相同的函数reverse_strings,但参数与第一次调用时不同;事实上,当你第一次调用它时,你会做这样的事情reverse_strings('hi').在 else 中,您使用较小版本的字符串 hi 调用函数,请注意以下语句:return reverse_strings(string[1:])+ string[0]string[1:] 就是 i.现在,基本上你有 return reverse_strings('i') + string[0].请注意,string[0]H.在这里,reverse_strings('i'),正如我上面所说的,你再次调用你的函数,但你的字符串i 的版本更小.

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 this reverse_strings('hi'). In the else, you are calling the function with a smaller version of your string hi, observe in the following statement: return reverse_strings(string[1:]) + string[0] that string[1:] is just i. Now, basically you have return reverse_strings('i') + string[0]. Note that string[0] is H. Here, reverse_strings('i'), as I said above, you are calling your function again, but with a smaller version of your string i.

现在,我们在另一个函数调用中.第一个语句 if len(string) == 0: return '' 被评估.这是真的吗?不,因为 len(string) 仍然与 0 不同.所以我们传递到下一个语句else: return reverse_strings(string[1:]) + string[0].现在,请注意我们将再次调用相同的函数.但是使用较小的字符串,在这种情况下,是一个空字符串,因为 istring[1:] 是一个空字符串.所以我们的调用可以总结如下return reverse_strings('') + i.

Now, we are inside another function call. The first statement if len(string) == 0: return '' is evaluated. Is it true? No, because len(string) is still different from 0. So we pass to the next statement else: 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, since string[1:] of i is an empty string. So our call can be summarised like this return reverse_strings('') + i.

我们现在处于 reverse_strings 的另一个简化版本".第一个语句被评估 if len(string) == 0: return ''.这是真的吗?是的,因为请记住,我们的字符串现在是一个空字符串.现在发生的是你向调用者返回一个空字符串,也就是第 3 点中的函数.

We are now in another "simplified version" of reverse_strings. The first statement is evaluated if 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.

现在,我们刚刚在第 3 点向调用者返回了一个空字符串,所以我们有 return '' + i.'' + i 只不过是简单的 i,您将在指针 3 处将其返回给此函数的调用者,也就是点 2 处的函数.

Now, we have just returned an empty string to the caller at point 3, so we have return '' + i. '' + i is nothing else than simply i, which you are going to return to the caller of this function at pointer 3, which is the function at point 2.

现在,您刚刚将 i 返回给调用者,具体来说,您刚刚将 i 返回到此语句 return reverse_strings('i')+ string[0],其中 string[0]Hreverse_strings('i') 就是 你刚回来.所以,现在你有了 iH,你刚刚反转了字符串.

Now, you have just returned i to the caller, specifically you have just returned i to this statement return reverse_strings('i') + string[0], where string[0] is H and reverse_strings('i') is just i that you have just returned. So, now you have iH, and you have just reversed the string.

这篇关于reverse_string 递归函数说明的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆