python-recursive function

Source: Internet
Author: User
Tags function definition

Recursive function definition: Call yourself in the function definition
    • Recursion is a self-invocation in a procedure or function.
    • Recursion must have a recursive exit, i.e. a recursive end condition

Give a chestnut-factorial:

def fact(n):    if==1:        return1    return*-1)print(factorial(5))# 120

function Execution Procedure:

===>Fact5)===> 5 *Fact4)===> 5 *(4 *Fact3))===> 5 *(4 *(3 *Fact2)))===> 5 *(4 *(3 *(2 *Fact1))))===> 5 *(4 *(3 *(2 * 1)))===> 5 *(4 *(3 * 2))===> 5 *(4 * 6)===> 5 *  -===>  -

Note: Using recursive functions requires preventing stack overflow. function calls are implemented through a stack of data structures, and each time a function call is entered, the stack adds a stack frame, and whenever the function returns, it is reduced by a stack frame. Because the stack space is limited, too many recursive calls can cause stack overflow

Tail-Recursive optimization:

In order to avoid stack overflow, can be optimized by tail recursion , the tail recursion is actually the same as the loop, we can loop as a special tail recursion.

Tail recursion means that when a function returns, it calls itself, and the return statement cannot contain an expression. In this way, the compiler or interpreter can optimize the tail recursion, so that the recursive itself, no matter how many times it is called, consumes only one stack frame, and no stack overflow occurs.

# 尾递归的例子def calc(n):    print-1)    if>-50:        return calc(n-1)

According to the definition of tail recursion, our factorial is not a tail recursion.

def fact(n):    if==1:        return1    return*-1)

Because the return statement is a multiplication operation, a return n * factorial(n - 1) multiplication operation is introduced.

  • At the end of a recursive call, if optimized, the stack does not grow, so no amount of calls will cause the stack to overflow.
  • The advantage of using a recursive function is that the logic is simple and clear, and the disadvantage is that too-deep calls cause a stack overflow.
  • The language that is optimized for tail recursion can prevent stack overflow by tail recursion. The tail recursion is actually equivalent to the loop, and the programming language without the loop statement can only be looped by the tail recursion.
  • The Python standard interpreter is not optimized for tail recursion, and there is a stack overflow problem with any recursive function. ^_^
Hanoi Tower Problem

Hanoi: Hanoi (also known as Hanoi) is a puzzle toy from an ancient Indian legend. When big Brahma created the world, he made three diamond pillars, and stacked 64 gold discs on a pillar from bottom to top in order of size. The great Brahma commanded the Brahman to rearrange the discs from below to the other pillars in order of size. It is also stipulated that the disc cannot be enlarged on the small disc, and only one disc can be moved between the three pillars at a time.

def move(n,a,b,c):    if==1:        print(‘move‘‘-->‘, c)    else:        move(n-1,a,c,b)        move(1,a,b,c)        move(n-1,b,a,c)move(3,‘A‘,‘B‘,‘C‘)# 打印结果:# move A --> C# move A --> B# move C --> B# move A --> C# move B --> A# move B --> C# move A --> C

python-recursive function

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.