Recursive functions of Python

Source: Internet
Author: User

Inside a function, you can call other functions. If a function calls itself internally, the function is a recursive function.

For example, let's calculate factorial n! = 1 * 2 * 3 * ... * n, denoted by the function fact (n) , you can see:

Fact (n) = n! = 1 * 2 * 3 * ... * (n-1) * n = (n-1)! * n = fact (n-1) * n

So,fact (n) can be represented as N * Fact (n-1), and only n=1 requires special handling.

So, fact (n) is written in a recursive way:

def fact (N):
If n==1:
return 1
Return n * Fact (N-1)

Above is a recursive function. You can try:

>>> Fact (1)
1
>>> Fact (5)
120
>>> Fact (100)
9332621544394415268169923885626670049071596826438162146859296389521759999322991560894146397615651

8286253697920827223758251185210916864000000000000000000000000L

If we calculate the fact (5), we can see the calculation process according to the function definition as follows:

===> Fact (5)
===> 5 * FACT (4)
===> 5 * (4 * FACT (3))
===> 5 * (4 * (3 * FACT (2)))
===> 5 * (4 * (3 * (2 * FACT (1)))
===> 5 * (4 * (3 * (2 * 1)))
===> 5 * (4 * (3 * 2))
===> 5 * (4 * 6)
===> 5 * 24
===> 120

The advantage of recursive functions is that they are simple in definition and clear in logic. In theory, all recursive functions can be written in a circular way, but the logic of the loop is not as clear as recursion.

use recursive functions to avoid stack overflow . In the computer, the function call is implemented through a stack (stack) of this data structure, each time into a function call, the stack will add a stack of frames, whenever the function returns, the stack will be reduced by a stack of frames. Because the size of the stack is not infinite, there are too many recursive calls that can cause the stack to overflow. You can try to calculate fact (10000).

Task

The movement of Hanoi (http://baike.baidu.com/view/191666.htm) can also be seen as a recursive function.

We have a column numbered A, B, C, and all the discs from a to C can be described as:

If a has only one disc, it can be moved directly to C;

If a has N disks, it can be seen as a has 1 discs (chassis) + (N-1) discs, the first need to move (N-1) a disk to B, and then, the last disk of a is moved to C, and then the B (N-1) disc moved to C.

Write a function, given the input n, a, B, C, to print out the moving steps:

Move (n, a, B, c)

For example, enter move (2, ' A ', ' B ', ' C ') to print out:

A-and B

A-and C

B--C

The definition of a function move (n, a, B, c) is to move n discs from A through B to C.

Reference code:

def Move (n, a, B, c):
    if n ==1 :
        print A, '--', C
Span style= "Mso-spacerun:yes" >        return
    Move (N-1, A, C, b)
     Print A, '--', C
    Move (n-1, B, A, c)
Move (4, ' A ', ' B ', ' C ')

A-and B
A-and C
B--C
A-and B
C--A
C--B
A-and B
A-and C
B--C
B--A
C--A
B--C
A-and B
A-and C
B--C

Debug Check out the recursive algorithm.

Recursive functions of Python

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.