Python Learning Note __2.4 recursive function

Source: Internet
Author: User

# This is a learning note for the Liaoche teacher Python tutorial


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

Write a factorial function using a recursive function:

def fact (N):

If n==1:

Return 1

return n * Fact (N-1)

Calculation process:

===> 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

Examples

The movement of Hanoi can be implemented very simply with recursive functions.

Write the Move (n, a, B, c) function, which receives the parameter n, which represents the number of plates of the 1th pillar A in the 3 pillars A, B, C, and then prints out the method of moving all the plates from A through B to C, for example:

#-*-Coding:utf-8-*-

def move (n, a, B, c):

If n==1:

Print (' Move ', A, '--', c) #圆盘个数为1时, move from A to C to complete, to terminate recursion

Else

Move (N-1,A,C,B) #把除了a柱子最下的一个圆盘以外的其他圆盘, moving to column B

Move (1,A,B,C) #把a柱子最下的圆盘 to the C pillar

Move (N-1,B,A,C) #把b柱子上的n-1 plates, move all to C pillar

Analytical:

N==1, print is the value of a-the value of "C"

First distinguishes between positional and value relationships . This function is constantly changing the corresponding values in each position, understanding the A,b,c and the relationship to its corresponding position

Then, when n=3, the position is denoted by uppercase letters, and the lowercase letters represent the true values

Move (3,A,B,C) at this point "a" = "a", "B" = "B", "C" = "C"

And then:

Move (2,A,C,B) # swaps the value of B,c, at which point "a" = "a", "B" = "C", "C" = "B"

Move (1,A,C,B) # again the value of B,c was swapped back a-"C, at this time" a "=" a "," B "=" B "," C "=" C "

Move (1,A,B,C) # At this time "a" = "a", "B" = "C", "C" = "B". So A-"b

Move (1,B,A,C) # At this point "a" = "C", "B" = "A", "c" = "B". So C-"b

Move (1,A,B,C) # At this time "a" = "a", "B" = "B", "C" = "C". So a-"C

Move (2,B,A,C) # At this point "a" = "B", "B" = "A", "c" = "C". So C-"b

Move (1,a,c,b) # At this time "a" = "B", "B" = "C", "c" = "a". So B-"a

Move (1,A,B,C) # At this point "a" = "B", "B" = "A", "c" = "C". So B-"C

Move (1,B,A,C) # At this time "a" = "a", "B" = "B", "C" = "B". So a-"C

Python Learning Note __2.4 recursive function

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.