Directory
I. Recursion
1. Definition of recursion
Recursion (recursion), which is also recursive, in mathematics and computer science, refers to the method of using the function itself in the definition of a function. The term recursion is also longer used to describe the process of repeating things in a self-similar way.
F0 = 0F1 = 1
2. Principle of recursion
(1). Examples:
Defth = = 5= A1 + defth + 1== recursion (1, 0, 1 (ret)
The following picture is the entire function of the execution process, the red represents a layer to the inside of the nested, the green represents the function of the return value of a layer of the outside return. In fact, recursion is this principle, through a function of the execution of the flow in the function again, when the return of a value by a condition, a layer of a layer after the execution of the first flow back again, and finally get the return value, but the recursive time to pay attention to two points:
1. His condition must be so that his recursion can return a value within a condition, otherwise it will always be recursive until the computer resources are exhausted (Python defaults to recursion limit)
2. The return value, in which the recursive function is generally to give him a certain return value, otherwise the last return of recursion will not get the desired value.
Two. Bubble sort
1. Bubble Sorting principle
Bubble sort is a simple sort algorithm. He repeatedly visited the sequence to sort, comparing two elements at a time, and swapping them out if they were in the wrong order.
The bubbling sorting algorithm works as follows: 1. Compares the adjacent elements. If the first one is bigger than the second one, swap them both. 2. Do the same work for each pair of adjacent elements, starting from the first pair to the end of the last pair. When this is done, the final element will be the maximum number. 3. Repeat the above steps for all elements except the last one. 4. Repeat the above steps each time for less and fewer elements until there are no pairs of numbers to compare
The principle of exchanging data
A peripheral variable is used to save the original value, and then to exchange data by a transform to the address. Note: When temp points to a, then a points to B, the point of the temp itself is not changed, as if a points to B, but temp still points to the address of a, so he's still 66.
A = 66b = 88temp = A A = b b = Temp
The principle of bubble sort
2. Bubble Sort Case
1 #-*-Coding:utf-8-*-2 # Zhou 3 # 2017/6/17 4 list = [0,,, 1] 5 for J in range (1, Len (list)): 6 For I in range (Len (list)-J): 7 # If the first data is large, then exchange data, otherwise, do not change 8 if list[i] > list[i + 1]: 9 temp = list[i]10 List[i] = list[i + 1]11 list[i + 1] = TEMP12 print (list)
Three. Decorative Device
1. Adorner definition
What is an adorner? It is simply a function that, without changing the source function code, is a subtle extension of the code to make its function further enhanced. An adorner is a function, a function that is loaded on top of other functions,
Let's look at a couple of concepts here first:
<1>. Function Execution Flow
From the above, the execution flow of the function should be from top to bottom, that is, the code first loads the first test1 into memory, and then opens a new memory to hold the second test1.
Here should be the point of the first test1 changed to 890673481792 here.
def test1 ():p rint (' Japanese. ') Print (ID (test1)) def test1 ():p rint (' Chinese. ') Print (ID (test1)) test1 () execution result: 890673481656890673481792 Chinese.
<2>. function as a variable
As can be seen from the following results, the function name is actually a variable that can be used to pass the variable.
Note: The function cannot be used as a variable without parentheses, it must be just the letter name
(=<function test1 at 0x000000e2d403c7b8><function test1 at 0x000000e2d403c7b8>
<3>. function nesting
Here define three functions, TEST1,TEST2,TEST3, 3 nested 1, 1 inside and nested 2, from his results presumably you will also see the execution flow of the function.
def test1 ():p rint (' I am ', end= ') def test2 ():p rint (' Chinese. ') Test2 () def test3 (): test1 () print (' Hello, China ') TEST3 () Result: I am Chinese. Hello, China.
2. Decorator principle
(1). The wording and use of adorners
<1>. Adorners are also a function
<2>. Use the adorner format: precede a function with the name of the @ adorner
(2). The principle of the adorner
<1>. Pass the Test1 function as a variable into the outer
Func = Test1
<2>. Assign a function inner nested in the adorner to Test1
Test1 = Inner
<3>. When executing the Test1 function, it is equivalent to executing the inner function, so the last test1 () command actually executes the inner, so first output (which country are you)
<4>. As the execution flow executes to the Func function, the original test1 function is actually executed, so the output (I am Chinese) is returned and the return value to the RET
<5>. After the original Test1 function is executed, the command inside the inner continues to execute, so the output (oh,hh, I love China.)
(3). Summary of adorners
As can be seen from the above execution flow, the adorner actually passes the previous function as a parameter, and then creates another function to add the required functionality before or after the original function.
(=((
3. Decorative Adorner with parameters
For the high availability of adorners, it is common to use the following method, that is, no matter how many parameters the function is used, this adorner can be used
Inside Python, it automatically assigns his parameters.
#-*-coding:utf-8-*-# zhou# 2017/6/17def outer (func):d EF Inner (A, *args, **kwargs):p rint (' What nationality are you? ') ret = func (A, *args, **kwargs) print (' Oh, hh, I love China. ') Return Inner@outerdef test1 (A, *args, **kwargs):p rint (' I'm Chinese. ') Test1 (1)
3. Nesting of adorners
<1>. Simplification of the first layer adorner (outer adorner)
<2>. Second-level adorner simplification (OUTER0 adorner)
<3>. Adorner nesting attack amount, we can find that a layer of adorners is actually nesting the original function in the middle of another function, so we just need a layer of peel-off nesting.
#-*-Coding:utf-8-*-# zhou# 2017/6/17def Outer0 (func):d EF inner ():p rint (' Hello, Kitty. ') ret = func () print (' I am Japanese. ') Return innerdef outer (func):d EF inner ():p rint (' What nationality are you? ') ret = func () print (' What about you? ') return inner@outer0@outerdef test1 ():p rint (' I am Chinese. ') Test1 () results Hello, Kitty. What nationality are you? I am a Chinese. What about you? I am a Japanese.