python,即使递归不应该允许递归允许递归,该指令如何完成?

发布于 2025-01-21 16:36:55 字数 587 浏览 0 评论 0原文

在此功能中:

def print_triangle (sideLength):
   if sideLength < 1 :
     return
   print_triangle (sideLength-1)
   print ( "[]"* sideLength)


指令:

print ( "[]"* sideLength)

永远不应达到指令,因为指令:

print_triangle (sideLength-1)

将阻止它在不断调用函数的情况下达到该功能,这将使程序的流程再次发送到函数的开头,也就是说顶部。 因此,它将继续执行此操作,

print ( "[]"* sideLength)

直到变量sidelength变为零,它将继续执行此操作。

然而,

print ( "[]"* sideLength)

当您将其称为4作为参数时,可以到达并绘制一个三角形...但是如何呢?

In this function:

def print_triangle (sideLength):
   if sideLength < 1 :
     return
   print_triangle (sideLength-1)
   print ( "[]"* sideLength)


The instruction:

print ( "[]"* sideLength)

Should never be reached, because the instruction:

print_triangle (sideLength-1)

Would be preventing it to be reached as it keeps calling the function, which would send the flow of the program again to the beginning of the function, that is to the top. Instead of allowing it to keep going below to the:

print ( "[]"* sideLength)

Hence, it will continue doing that until the variable sideLength becomes zero.

And yet the line:

print ( "[]"* sideLength)

Is reached and draws a triangle when you call it with say a 4 as a parameter... but how?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

维持三分热 2025-01-28 16:36:55

您可以想象下面的递归呼叫“树”。每个框代表函数的执行上下文,因此每次进行新调用时,都会描绘一个新框。内部最大的框表示参数为0时的情况,在这种情况下,函数将返回,关闭内部框。但是,该最终框的呼叫者将能够继续,并继续使用print指令。

因此,这是最初的电话:

print_triangle(4)
┌───────────────────────────────────────────────────────┐
│ sideLength == 4                                       │
│ print_triangle(3):                                    │
│    ┌──────────────────────────────────────────────┐   │
│    │ sideLength == 3                              │   │
│    │ print_triangle(2):                           │   │
│    │    ┌─────────────────────────────────────┐   │   │
│    │    │ sideLength == 2                     │   │   │
│    │    │ print_triangle(1):                  │   │   │
│    │    │    ┌────────────────────────────┐   │   │   │
│    │    │    │ sideLength == 1            │   │   │   │
│    │    │    │ print_triangle(0):         │   │   │   │
│    │    │    │    ┌───────────────────┐   │   │   │   │
│    │    │    │    │ sideLength == 0   │   │   │   │   │
│    │    │    │    │ return            │   │   │   │   │
│    │    │    │    └───────────────────┘   │   │   │   │
│    │    │    │ print("[]"*1)              │   │   │   │
│    │    │    └────────────────────────────┘   │   │   │
│    │    │ print("[]"*2)                       │   │   │
│    │    └─────────────────────────────────────┘   │   │
│    │ print("[]"*3)                                │   │
│    └──────────────────────────────────────────────┘   │
│ print("[]"*4)                                         │
└───────────────────────────────────────────────────────┘

You can imagine the recursive call "tree" as below. Each box represents an execution context of the function, so each time a new call is made a new box is depicted. The inner most box represents the case when the argument is 0, in that case the function will return, closing the inner box. But then the caller of that innermost box will be able to continue, and it will continue with the print instruction.

So here is the initial call:

print_triangle(4)
┌───────────────────────────────────────────────────────┐
│ sideLength == 4                                       │
│ print_triangle(3):                                    │
│    ┌──────────────────────────────────────────────┐   │
│    │ sideLength == 3                              │   │
│    │ print_triangle(2):                           │   │
│    │    ┌─────────────────────────────────────┐   │   │
│    │    │ sideLength == 2                     │   │   │
│    │    │ print_triangle(1):                  │   │   │
│    │    │    ┌────────────────────────────┐   │   │   │
│    │    │    │ sideLength == 1            │   │   │   │
│    │    │    │ print_triangle(0):         │   │   │   │
│    │    │    │    ┌───────────────────┐   │   │   │   │
│    │    │    │    │ sideLength == 0   │   │   │   │   │
│    │    │    │    │ return            │   │   │   │   │
│    │    │    │    └───────────────────┘   │   │   │   │
│    │    │    │ print("[]"*1)              │   │   │   │
│    │    │    └────────────────────────────┘   │   │   │
│    │    │ print("[]"*2)                       │   │   │
│    │    └─────────────────────────────────────┘   │   │
│    │ print("[]"*3)                                │   │
│    └──────────────────────────────────────────────┘   │
│ print("[]"*4)                                         │
└───────────────────────────────────────────────────────┘
你又不是我 2025-01-28 16:36:55

好吧,想一想。

def print_triangle (sideLength):
   if sideLength < 1 :
     return
   print_triangle (sideLength-1)
   print ( "[]"* sideLength)
print_triangle (4)

首先,它以4个开始

if 4 < 1 :
     return
print_triangle (4-1)
print ( "[]"* 4)

,在打印[]*4之前,然后再次调用函数,这次是3:

if 3 < 1 :
     return
print_triangle (3-1)
print ( "[]"* 3)

现在,在打印[]*3 <之前,它是 3:现在。 /code>,然后再次调用函数,这次是2:

if 2 < 1 :
     return
print_triangle (2-1)
print ( "[]"* 2)

现在,在它打印[]*2之前,然后再次调用

if 1 < 1 :
     return
print_triangle (1-1)
print ( "[]"* 1)

函数打印[]*0,然后再次调用函数,这次以0为0。

现在,我们知道零小于1,它返回到最后一个功能,或1。

现在我们去向后回到最后一个功能调用。

print ( "[]"* 1)

然后是在此之前的一个:

print ( "[]"* 2)

然后是一个之前:

print ( "[]"* 3)

最后您第一次呼叫该功能:

print ( "[]"* 4)

Well, think it through.

def print_triangle (sideLength):
   if sideLength < 1 :
     return
   print_triangle (sideLength-1)
   print ( "[]"* sideLength)
print_triangle (4)

First, it starts with 4

if 4 < 1 :
     return
print_triangle (4-1)
print ( "[]"* 4)

Now, before it prints off []*4, it then calls the function again, this time for 3:

if 3 < 1 :
     return
print_triangle (3-1)
print ( "[]"* 3)

Now, before it prints off []*3, it then calls the function again, this time for 2:

if 2 < 1 :
     return
print_triangle (2-1)
print ( "[]"* 2)

Now, before it prints off []*2, it then calls the function again, this time for 1:

if 1 < 1 :
     return
print_triangle (1-1)
print ( "[]"* 1)

Now, before it prints off []*0, it then calls the function again, this time for 0.

Now we know that since zero is less than 1, it returns to the last function, or 1.

Now we go backwards to the last function call.

print ( "[]"* 1)

Then to the one before that:

print ( "[]"* 2)

And then the one before that:

print ( "[]"* 3)

And finally your first call of the function:

print ( "[]"* 4)
子栖 2025-01-28 16:36:55

它执行 返回基本案例,并且功能调用的堆栈开始弹出。

如果按照以下方式写入它,则交换最后两行:

def print_triangle (sideLength):
   if sideLength < 1 :
     return
   print ( "[]"* sideLength)
   print_triangle (sideLength-1)

区别将是打印的三角形将颠倒。

It executes after the base case is returned and the stack of function calls start to pop.

If you write it as follows, swapping the last two lines:

def print_triangle (sideLength):
   if sideLength < 1 :
     return
   print ( "[]"* sideLength)
   print_triangle (sideLength-1)

The difference will be that the printed triangle will be upside down.

作妖 2025-01-28 16:36:55

指令:

  print(“ []”* sidelength)
 

永远不应该达到,因为指令:

  print_triangle(sidelength-1)
 

将阻止它到达,因为它不断调用功能

是不正确的。首先,“它不断调用功能”是错误的。这仅调用一次函数。当功能返回时,它将继续到下一行。

特别是,

   if sideLength < 1 :
     return

只要sidelength到达0函数调用将返回。

作为一个更具体的示例,让我们看看当您调用print_triangle(3)时会发生什么。首先,我们检查如果sidelength&lt; 1是错误的,因此我们转到print_triangle(sidelength-1)

这是递归调用print_triangle(2)。同样,我们检查如果Sidelength&lt; 1。仍然跌倒,因此我们再次使用print_triangle(1)再次浏览。 如果Sidelength&lt; 1仍然是错误的,因此我们再用print_triangle(0)再一次重复一次。

但是现在如果sidelength&lt; 1是真的,所以我们返回。这使我们回到了sidelength的呼叫中,但在递归呼叫之后。要执行的下一行代码是print(“ []* sidelength),这是执行的方式。

打印后,我们再次从函数返回到sidelength为2。到3。然后再打印,然后再从顶级呼叫返回。

The instruction:

print ( "[]"* sideLength)

Should never be reached, because the instruction:

print_triangle (sideLength-1)

Would be preventing it to be reached as it keeps calling the function

This is incorrect. First, "it keeps calling the function" is wrong. This only calls the function once. When the function returns, it will continue to the next line.

In particular, you have

   if sideLength < 1 :
     return

So as soon as sideLength reaches 0 the function call will return.

As a more concrete example, let's look what happens when you call print_triangle(3). First, we check if sideLength < 1, which is false, so then we go to print_triangle (sideLength-1).

This is the recursive call print_triangle(2). Again, we check if sideLength < 1. Still falls, so we recurse again with print_triangle(1). if sideLength < 1 is still false, so we recurse one more time with print_triangle(0).

But now if sideLength < 1 is true, so we return. This puts us in back to the call where sideLength is 1, but after the recursive call. The next line of code to execute is print ( "[]"* sideLength), which is how it gets executed.

After the print, we return from the function again back to when sideLength is 2. So we print() again and return back to sideLength set to 3. And one more print before returning from the top-level call.

书间行客 2025-01-28 16:36:55

如果您不返回值,则该函数将继续运行,然后重复。所以移动:
返回print_triangle(sidelength-1)

print> print(“ []”* sidelength)。将使print()仅运行一次

If you don't return the value then the function will continue running it will then repeat. So moving:
return print_triangle (sideLength-1)

Before print( "[]"* sideLength). Will make print() only run once

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文