Python recursive functions
In this article, we will learn from the following blog to learn more about recursion.
Http://www.cnblogs.com/alex3714/articles/5740985.html)
1. Recursive Function Definition
If a function calls itself within a function, this function is a recursive function. For example:
def sum(arg,stop): print(arg) if arg<stop: arg+=arg sum(arg,stop)sum(1,20)
2. Recursive features
1) there must be a clear ending condition, otherwise it will enter an endless loop.
2) each time you enter a deeper layer of recursion, the problem scale should be reduced compared to the previous recursion.
3) The recursion efficiency is not high, and too many recursive layers will cause stack overflow (in the computer, function calls are implemented through the stack data structure. Whenever a function is called, the stack adds a stack frame. Every time the function returns, the stack will subtract a stack frame. Because the stack size is not infinite, too many recursive calls will cause stack overflow)
For example, if the return value is returned in the previous example, the original stack value is returned:
1 def sum (arg, stop): 2 print (arg) 3 if arg <stop: 4 arg + = arg 5 sum (arg, stop) 6 return arg 7 8 print (sum (112) 9 10 # Run the result 11 213 414 815 1616 3217 2 # return a layer-by-layer returned arg value, and finally return the initial stack Value
Demo
3. Apply recursion to obtain the Fibonacci sequence
The Fibonacci series refers to a group of numbers that combine the first two numbers to form a third number rule, for example, 0, 1, 2, 3, 5, 8, 13.
1 def func (arg1, arg2, stop): 2 if arg1 = 0: 3 print (arg1, arg2) 4 arg3 = arg1 + arg2 5 print (arg3) 6 if arg3 <stop: 7 func (arg2, arg3, stop) 8 9 func (113, 10) 10 11 # running result 12 0 114 215 316 517 818 13
Demo
4. implement binary search using recursion
When there is a large amount of data to search for, it is appropriate to use the binary method. When binary search is used, the data needs to be sorted, and half of the data is compared with the search value to reduce the search time.
1 def binary_search (data_source, find_n): 2 mid = int (len (data_source)/2) 3 if len (data_source)> 1: 4 if data_source [mid]> find_n: # data in left 5 print ('data in left of [% s] '% data_source [mid]) 6 binary_search (data_source [: mid], find_n) 7 elif data_source [mid] <find_n: # data in right 8 print ('data in left of [% s] '% data_source [mid]) 9 binary_search (data_source [mid:], find_n) 10 else: 11 print ('found find_s: ', data_source [mid]) 12 else: 13 print ('cannot find .... ') 14 15 if _ name __= =' _ main _ ': 16 data = list (range (36378) 17 binary_search (data) 18 19 # running result 20 data in left of [25000] 21 data in left of [37500] 22 data in left of [31250] 23 data in left of [34375] 24 data in left of [35937] 25 data in left of [36718] 26 data in left of [36327] 27 data in left of [36522] 28 data in left of [36424] 29 data in left of [36375] 30 data in left of [36399] 31 data in left of [36387] 32 data in left of [36381] 33 found find_s: 36378Demo