Python Day 15 (recursive function, binary lookup algorithm)

Source: Internet
Author: User

Python Day 15 (recursive function, binary lookup algorithm) recursive function

In a function, the function itself is called.

Default maximum depth of recursion: 998

Modify default Maximum Depth

Import Sysprint (Sys.setrecursionlimit (100000))
ImportSyssys.setrecursionlimit (1000000) Count= 1defMy_func ():GlobalCountPrint(count) Count+ = 1My_func () my_func ( )===========================defMy_age (n,start=23):      ifn = = 2:        returnStartelif0 < N < 2:        returnMy_age (n + 1)-2elifn > 2 :        returnMy_age (n-1) + 2Else:        return 'does not exist. 'Print(Eval ('My_age') (2))

Binary search algorithm
  a chance, I think of before Google work, sometimes people will be at the table to discuss the latest thought out some of the questions. In many interesting and difficult topics, there is an old problem is that we have to choose to avoid, that is to achieve two points to find. Because it is very good to write, but it is difficult to write right. Can imagine asked this question, in 5 minutes to interview the reunion very confident to the small piece of code to us, the rest is to test whether the interviewer can see in a shorter period of time the code of the bug. What is a binary search, this is not just a programmer, but many other non-technical people will. For example, I want a number within 1 to 100, you guess, I tell you every time the guess is big or small, you will first guess 50, then 25, then ... You can guess with a few questions. 1 to 100 the range is too small, we enlarge the point to guess the personal name, you ask Chinese foreigners, ancient people modern, male female, can not use a few questions also asked out. In the computer, it is in an ordered array, and continuously through the two-point method to narrow the keyword's possible subscript range. Of course, we do not necessarily look in an ordered array, or in a large state space, to find the value of a monotone function. Such a procedure seems to be easy to implement, but D.knuth said: Although the basic idea of binary search  is   comparatively straightforward, the details can be surprisingly tricky Although the basic idea of binary search is relatively straightforward, there are particularly many pits that are implemented in concrete. Another great God, the author of programming Zhu Ji Nanxiong Jon Bentley, did what we didn't dare to do at the beginning of the article, and he arranged his homework to get his students to write two-point searches, and then he looked at it one by one. As a result, he found that the %

  

If you have a list that lets you find 66 of the locations from this list, what do you want to do?

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]

defSearch (num,l,start=none,end=None): Start= StartifStartElse0 End= EndifEndElseLen (L)-1Mid= (End-start)//2 +StartifStart >End:returnNoneelifL[mid] >Num:returnSearch (num,l,start,mid-1)    elifL[mid] <Num:returnSearch (num,l,mid+1, end)elifL[mid] = =Num:returnMid

Python Day 15 (recursive function, binary lookup algorithm)

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.