The recursive and dichotomy method of Day5-python

Source: Internet
Author: User

First, the definition of recursion
A recursive call is a special form of a function nested call that invokes itself directly or indirectly when called, or recursively.
second, recursion is divided into two stages: recursion, backtracking
Age (5) = Age (4) + 2age (4) = Age (3) + 2age (3) = Age (2) + 2age (2) = Age (1) + 2age (1) = 18age (n) =age (n-1) +2 #n >1age (1) =18 #n =1######################### #3def Age (N):    if n = = 1: Return return (    n-1) + 2print (age (5))
Third,the recursive efficiency of Python is low and no tail recursive optimization
#python中的递归python中的递归效率低, the current state needs to be preserved at the next recursion, and there can be a workaround in other languages: tail recursion optimization, which is called itself at the last step of the function (not the last line), and the tail recursion is optimized:/HTTP egon09.blog.51cto.com/9161406/1842475 but Python has no tail recursion, and the recursive hierarchy is limited by the use of summary recursion: 1. There must be a definite end condition of 2. Each time you enter a deeper level of recursion, the problem size should be reduced by 3 compared to the last recursion. Recursive efficiency is not high, too many recursive hierarchy will lead to stack overflow (in the computer, function calls through the stack (stack) This data structure implementation, whenever entering a function call, the stack will add a stack of frames, whenever the function returns, the stack will be reduced by a stack of frames. Because the size of the stack is not infinite, there are too many recursive calls, which can cause the stack to overflow.
iv. Recursive maximum depth can be modified
Import Syssys.getrecursionlimit () Sys.setrecursionlimit (+) N=1def Test ():    Global n    print (n)    n+=1    test () test () Although it can be set, but because it is not the tail recursion, still want to save the stack, memory size must be, not infinite recursion
V: Recursive application
Application: Take out all the contents of the list L = [1, 2, [3, [4, 5, 6, [7, 8, [9], [n], [+], [+], [+], [+]], 19]]]]]]]def search (L): for    Item in L:        if Type (item) is list:            search (item)        Else:            print (item) search (L)
six or two-point method

If you want to find the number specified in a list of numbers from small to large, the efficiency of the traversal is too low, a dichotomy (one of the algorithms, an algorithm that solves the problem) can greatly reduce the size of the problem.

1.Find a number that's not in the list.
L = [1, 2, 5, 7, ten, 102, N, M, M, 240]+++++++++++++++++++++++++++++++++++a=len (L)//2print (a) #6print (Len ( L)) #13print (l[6:]) #[44, M, M, a, 102, 240]print (L[:6]) #[1, 2, 5, 7, 31]+++++++++++++++++++++++++++++++++++de F Binary_search (L, num):    print (L)  # [Ten]    if Len (l) > 1:   #如果列表的长度大于一, perform the following recursive operation        Mid_index = Len (L)//2  # Divide the list into two halves and get the median index 44 with an index of 6        if num > L[mid_index]:  # The value that the user is looking for is compared to the median value, and if it is greater than the middle value, the value is on the right side            of the list. L = L[mid_index:]  # l=[31], slice, go to the right to find the value            binary_search (l, num)  # Recursive once, scale down by half        elif Num < L[mid_index]:  # If it is less than the median, then this value is on the left of the list            L = l[:mid_index]  # shard            binary_search (L, num)        else:            print (' Find it ')    else:        if l[0] = = num:# #如果列表中就存在一个值, if this value is the value I'm looking for, print            (' find it ') else directly        :            Print (' not exist ')        Returnbinary_search (L, 32)
2, find a number in the list is not optimized version
L = [1, 2, 5, 7, ten, 1,, 102, 240]def, Binary_search (L, num):    print (L)    if Len (l) = =:        L[0] = = num:            print (' Find it ')        else:            print (' Not EXISTS ')        return    mid_index = Len (l)//2    mid_ Value = L[mid_index]    if num = = Mid_value:        print (' Find it ')        return    if num > mid_value:        l = L[mi D_index:]    if num < mid_value:        L = l[:mid_index]    binary_search (l, num) Binary_search (L, 32)

  

  

The recursive and dichotomy method of Day5-python

Related Article

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.