Recursive functions
Primary knowledge recursive function
Definition of recursive function: Call the function itself in a function
Python has a recursive depth limit in order to consider the protection of memory consumption.
Explore the default maximum depth of recursion:
def foo (n): Print (n) + = 1 foo (n) foo (1)
Forced to control the number of recursive layer in the 997, and then error, error is just the computer in order to protect memory. Of course, 997 is a default value that Python sets for the memory optimization of our program, and of course we can modify it by some means:
Import SYS Print (Sys.setrecursionlimit (100000= 0def story (): Global n + = 1 print(n) Story () Stories ()
Output will not error, but there will be an automatic exit value, this value is suitable for the computer's own configuration, the general situation is 3222,mac will be larger.
Advantages and disadvantages of recursion
Disadvantage: Compared to occupy memory, if you need too many recursion, it is not appropriate to use recursive algorithm to solve the problem.
Pros: It makes the code simple
People understand loops and God understands recursion.
Solving the problem and understanding recursion
Example one:
Now you ask me, Alex, how old is the teacher? I said I wouldn't tell you, but Alex is two years older than Egon.
You want to know how old Alex is, do you have to ask Egon? Egon said, I do not tell you, but I am a tournament sir two years old.
You asked Sir Wu, and the officer did not tell you, he said he was two years older than Jinxin.
Then you ask Jinxin, Jinxin told you, he is 40 ...
Did you know that at this time? How old is Alex?
1 |
Jinxin |
40 |
2 |
Sir Wu |
42 |
3 |
Egon |
44 |
4 |
Alex |
46 |
Why would you know that?
First of all, you asked Alex's age, and found Egon, Wu Sir, jinxin, you asked ask the past, until you get an exact answer, and then follow this line to find back, only to get the final Alex's age. This process is already very close to recursive thinking.
We'll come to the concrete I analyze the rules between these few people.
Age (4) = Age (3) + 2 age (3) = Age (2) + 2 Age(2) = Age (1) + 2age (1) = 40
We write a recursive function, which is:
def Age (N): if n = =1 :return , else: return age (n-1 ) +2Print(age (4))
Analyze this recursive function in steps
defAge (1): if1 = = 4: return40elif1 >0 and1 < 4: returnAge (+ 2)defAge (2): if2 = = 4: return40elif2 >0 and2 < 4: returnAge (2+1) + 2defAge (3): if3 = = 4: return40elif3 >0 and3 < 4: returnAge (3+1) + 2defAge (4): if4 = = 4: return40elif3 >0 and3 < 4: returnAge (3+1) + 2
Example two: Understanding recursion with the dichotomy method
Binary lookup algorithms must process an ordered list
For example, there is now a list, looking for a specified number, how to achieve it by dichotomy
defFind (L,aim,start = 0,end =None): End= Len (l)ifEnd isNoneElseEnd Mid_index= (End-start)//2 +StartifStart <=End:ifL[mid_index] <aim:returnFind (L,aim,start =mid_index+1,end=end)elifL[mid_index] >aim:returnFind (L, aim, Start=start, end=mid_index-1) Else: returnMid_indexElse: return 'This value is not found'L= [2,3,5,10,15,16,18,22,26,30,32,35,41,42,43,55,56,66,67,69,72,76,82,83,88]ret= Find (l,66)Print(ret)
Python full stack development--recursive function