即使它不调用自身,该函数是否也是递归的? [英] Is this function recursive even though it doesn't call itself?
问题描述
from pythonds.basic.stack import Stack
rStack = Stack()
def toStr(n,base):
convertString = "0123456789ABCDEF"
while n > 0:
if n < base:
rStack.push(convertString[n])
else:
rStack.push(convertString[n % base])
n = n // base
res = ""
while not rStack.isEmpty():
res = res + str(rStack.pop())
return res
print(toStr(1345,2))
我指的是本教程并粘贴了代码以上.该教程说该函数是递归的,但我在任何地方都没有看到递归调用,只是一个 while 循环.我错过了什么?
I'm referring to this tutorial and also pasted the code above. The tutorial says the function is recursive but I don't see a recursive call anywhere, just a while loop. What am I missing?
推荐答案
这个特定函数不是递归是对的.然而,上下文是,在上一张幻灯片上有一个递归函数,在这张幻灯片中,他们想一瞥它的内部行为.他们后来说:
You are right that this particular function is not recursive. However, the context is, that on the previous slide there was a recursive function, and in this one they want to show a glimpse of how it behaves internally. They later say:
前面的例子 [即有问题的那个 - B.] 让我们深入了解 Python 如何实现递归函数调用.
The previous example [i.e. the one in question - B.] gives us some insight into how Python implements a recursive function call.
所以,是的,标题具有误导性,它应该是扩展递归函数或用堆栈模仿递归函数行为或类似的东西.
So, yes, the title is misleading, it should be rather Expanding a recursive function or Imitating recursive function behavior with a stack or something like this.
有人可能会说这个函数在某种意义上采用递归方法/策略来解决正在解决的问题,但它本身并不是递归的.
One may say that this function employs a recursive approach/strategy in some sense, to the problem being solved, but is not recursive itself.
这篇关于即使它不调用自身,该函数是否也是递归的?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!