简体   繁体   English

递归打印字符串,每次递增

[英]Recursively printing string backwards, increment each time

I have this function here that will print a string until it gets to the last character. 我在这里有这个函数,它将打印一个字符串,直到它到达最后一个字符。 The thing is I need it to do the opposite of that. 问题是我需要它做相反的事情。

Instead of 'abc' printing 'abc, ab, c' I need it to print 'c, bc, abc'. 而不是'abc'打印'abc,ab,c'我需要它来打印'c,bc,abc'。

Thank you! 谢谢!

def reverse(string):
  if len(string) == 0:
    return string
  else:
    print(string)
    return reverse(string[1:])

Just switch the 2 lines in the else. 只需在else中切换2行即可。 This will cause all of the recursive calls to happen before any of the print statements. 这将导致所有递归调用任何print语句之前发生。 Once the end of the string is reached, the recursion will unwind and all the print s will happen: 一旦到达字符串的末尾,递归将展开并且所有print将发生:

def reverse(string):
    if not string:
        return
    else:
        reverse(string[1:])
        print(string)

reverse("abc")

Output: 输出:

c
bc
abc

The base case here is easy: the length of the string is 0, simply return without printing. 这里的基本情况很简单:字符串的长度为0,只需返回而不打印。 If it isn't the base case, you have two things to do, keep breaking the string down ( recursively ), or printing the string. 如果它不是基本情况,你有两件事要做,继续打破字符串( 递归 ),或打印字符串。 Depending on the order, you can get the results printed in a different order : 根据订单,您可以按不同的顺序打印结果:

>>> def rev(string):
...     if string:
...         rev(string[1:]) # keep breaking down the string
...         print(string) # print the string
...         return
...     else:
...         return
...
>>> rev('abc')
c
bc
abc

Now look if we switch: 现在看看我们是否切换:

>>> def rev(string):
...     if string:
...         print(string) # print the string
...         rev(string[1:]) # keep breaking down the string
...         return
...     else:
...         return
...
>>> rev('abc')
abc
bc
c

Note, you don't really need the else: return , or the return after at the end of the if block, but I kept it in just to be explicit and make you think about what is happening in your function. 注意,你真的不需要else: return ,或者在if块结尾之后return ,但我保留它只是为了明确并让你思考你的函数中发生了什么。

Think of yourself as the first, intended version of the function. 将自己视为该功能的第一个预期版本。 I receive a string as an argument, and it isn't empty. 我收到一个字符串作为参数,它不是空的。 I then create a duplicate of myself (since we are imagining things) then break down my string and hand it off to him, giving him "control". 然后我创建了一个自己的副本(因为我们正在想象事情)然后分解我的字符串并将其交给他,让他“控制”。 And he does the same thing: checks if the string is not empty, breaks it down if that is the case, and creates a clone and gives him the new string and control, all the way down until we reach a clone who receives an empty string. 并且他做了同样的事情:检查字符串是否为空,如果是这样则将其分解,并创建一个克隆并给他新的字符串和控件,一直向下直到我们到达一个收到空的克隆串。 This clone simply checks and sees that the string is empty, and returns control to the clone that called him. 此克隆只是检查并看到该字符串为空, 并将控制权返回给调用他的克隆。 This clone now does the next step: he prints , and this clone is the clone that got a string with length one... after he is done printing, he returns control to the clone that called him... 这个克隆现在进行下一步:他打印 ,这个克隆是得到一个长度为1的字符串的克隆...在他完成打印后,他将控制权返回给称为他的克隆...

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM