Understanding recursive string reversal

前端 未结 2 1517
野的像风
野的像风 2021-01-27 16:35

I\'m using the Python Tutorial visualize webpage to try and understand the flow of this function that reverses a string:

text = \"hello\"
def reverse(text):
            


        
2条回答
  •  再見小時候
    2021-01-27 17:02

    reverse("hello")
    -> reverse("ello") + "h"
    -> reverse("llo") + "e" + "h"
    -> reverse("lo") + "l" + "e" + "h"
    -> reverse("o") + "l" + "l" + "e" + "h"
    -> "o" + "l" + "l" + "e" + "h"
    -> "olleh"
    

    This is to show the purpose of the return reverse(text[1:]) + text[0] line and its behaviour in action.

提交回复
热议问题