Python Study Notes-day 4, recursive functions (factorial and tower games), python Study Notes

Source: Internet
Author: User

Python Study Notes-day 4, recursive functions (factorial and tower games), python Study Notes

Today I learned mainly about recursive functions and have tried some small examples. Here I will record them with the factorial and tower of death.

1. factorial Functions

Factorial is very simple, that is, n! = 1x2x3x... xn.

Use a common iteration function to write a factorial. The Code is as follows:

1 def factorial (x): 2 for x in range (1, x + 1): 3 if x = 1: 4 y = 1 5 else: 6 y = y * x 7 return y 8 9 temp = int (input ('Please enter a number: ') 10 print (temp) 11 if temp = 0: 12 print ('0 has no factorial! ') 13 else: 14 temp1 = factorial (temp) 15 print ("% d's factorial is % d" % (temp, temp1 ))

The logic is simple, and variable y is constantly iterated. Because we are learning recursion, we use recursive methods to write it again.

The principle is, n! = N x (n-1 )!

1 def factorial (n): 2 if n = 1: 3 return 1 4 else: 5 return n * factorial (n-1) # That is, n! = N * (n-1 )! 6 7 number = int (input ('enter a positive integer ') 8 print (number) 9 result = factorial (number) 10 print ("% d's factorial is: % d "% (number, result ))

If we use recursion to write data, we feel that this function is much clearer.

2. Tower games

Background: The French mathematician Eduard Lucas once wrote an ancient Indian legend: In the holy temple at the center of the world, benalrus (in Northern India), three gem needles are inserted on a brass plate. In the creation of the world, Fan Tian, the Hindu god, wore 64 gold tablets from the ground up to the ground on one of the needles. No matter day or night, there is always a monk moving the gold Tablets according to the following rules: one piece at a time, no matter which needle, the small pieces must be on the large film. The monks predicted that the world would be wiped out in a bang when all the gold pieces were moved from the needle worn by fan Tian to another, and the Vatican, temples, and sentient beings will all be lost together.

Simply put, there are three columns (a, B, c) and n plates on column a. The plates from top to bottom are small to large, move the n plates from a to c, and the large ones are not allowed to be placed on the small ones.

When thinking about this problem, I think of it as a three-step process: think of the n plates on the axis as two parts, the largest one at the bottom, and other (n-1) dishes.

1. If you use it as two parts, the first step is to talk about the (n-1) plate above and move it to the B axis;

2. Step 2. Move the remaining part of the axis, namely the last plate, from the axis to the c axis;

3. Step 3: Move n-1-1 plate on B to c.

 

Then, based on the above three steps, the code written is as follows:

Def hanoi (n, a, B, c): if n = 1: # if there is only one disk, this disk is taken from the axis (start axis) move to the c axis (target axis ). Print (a, '-->', c) else: # If there are n plates hanoi (n-1), a, c, B) # Then step 1, that is, first move the (n-1) plates from the axis to the B axis, and use the c axis as the buffer. In this case, the axis is the start axis, the B axis is the target axis, and the c axis is the buffer axis. Hanoi (1, a, B, c) # Step 2, after moving (n-1) dishes, there is still one on the axis, that is to move the last one from the axis to the c axis. That is, hanoi (1, a, B, c), a --> c. Hanoi (n-1), B, a, c) # Step 3: Move the (n-1) plates on the B axis from the B axis to the c axis. At this time, B is the start axis, a is the buffer axis, and c is the target axis, that is, hanoi (n-1), B, a, c) n = int (input ('Enter the tower level ')) hanoi (n, 'A', 'B', 'C ')

In this way, after entering a number, you can get the desired tower movement.

Compile and try

Enter the tower level 3A --> CA --> BC --> BA --> CB --> AB --> CA --> C

The result is correct.

 

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.