English 中文(简体)
pthon recursion i 有意理解这一产出pl有助于我
原标题:python recursion i dont understand this output pls help me
def azalt(s):
    if len(s)< 1:
        return s
    else:
        print( özyinelemesürecine girerken: , s)
        azalt(s[1:])
        print( özyinelemesürecinden çıkarken: , s)

azalt( 12345 )

产出:

özyinelemesürecine girerken: 12345
özyinelemesürecine girerken: 2345
özyinelemesürecine girerken: 345
özyinelemesürecine girerken: 45
özyinelemesürecine girerken: 5 #(***)
özyinelemesürecinden çıkarken: 5 
özyinelemesürecinden çıkarken: 45
özyinelemesürecinden çıkarken: 345
özyinelemesürecinden çıkarken: 2345
özyinelemesürecinden çıkarken: 12345

i. 在<代码>#(***)(i.

i 预期产出就这样看。

产出:

özyinelemesürecine girerken: 12345
özyinelemesürecine girerken: 2345
özyinelemesürecine girerken: 345
özyinelemesürecine girerken: 45
özyinelemesürecine girerken: 5
最佳回答

For better understanding you can add another parameter to the function (lets call it level=) that will increase by 1 when you call the function recursively:

def azalt(s, level):
    if len(s) < 1:
        return s
    else:
        print("the string before:", s, f"{level=}")
        azalt(s[1:], level + 1)
        print("the string after:", s, f"{level=}")


azalt("12345", level=1)

结果是:

the string before: 12345 level=1
the string before: 2345 level=2
the string before: 345 level=3
the string before: 45 level=4
the string before: 5 level=5
the string after: 5 level=5
the string after: 45 level=4
the string after: 345 level=3
the string after: 2345 level=2
the string after: 12345 level=1

参看第二版<代码>(>>( after)在你从休养职能返回时执行。

问题回答

If you are not familiar with a recursion call stack, I would encourage you to look into it for fully understanding the print statements. Basically, each time azalt is being called recursively, it preserves the most recent line it executed and goes straight to the next call. When the next call returns, it executes the line right after it (keep in mind, it knows which line to execute because it is preserved in the stack). When it is done executing all the next steps, it returns.

电话: azalt(12345) [prints 12345, Next calls, the current calls on ack]

电话2:azalt(2345) [打印2345,下调,目前打脚印]

电话3:azalt(345) [打印345,下调,目前打脚]

第4号:azalt(45)[第45号,下调,目前呼吁打字]

电话5:azalt(5) [印数5,下电话:当前打字]

电话6:azalt(发泄) [恢复和从警钟中删除]

电话5:azalt(5) [现在印有5份,返回,从警钟中删除]

电话4:azalt(45) [现在印有45部,返回并搬出电线]

电话3:azalt(345) [现在印有345部,返回并拆除电话亭]

电话2:azalt(2345) [现在印有2345页的文字,返回并搬出电线]

call 1: azalt(12345) [now prints 12345, return and remove from the call stack]

希望这一帮助!





相关问题
Get webpage contents with Python?

I m using Python 3.1, if that helps. Anyways, I m trying to get the contents of this webpage. I Googled for a little bit and tried different things, but they didn t work. I m guessing that this ...

What is internal representation of string in Python 3.x

In Python 3.x, a string consists of items of Unicode ordinal. (See the quotation from the language reference below.) What is the internal representation of Unicode string? Is it UTF-16? The items ...

What does Python s builtin __build_class__ do?

In Python 3.1, there is a new builtin function I don t know in the builtins module: __build_class__(...) __build_class__(func, name, *bases, metaclass=None, **kwds) -> class Internal ...

what functional tools remain in Python 3k?

I have have read several entries regarding dropping several functional functions from future python, including map and reduce. What is the official policy regarding functional extensions? is lambda ...

Building executables for Python 3 and PyQt

I built a rather simple application in Python 3.1 using PyQt4. Being done, I want the application to be distributed to computers without either of those installed. I almost exclusively care about ...

热门标签