The path to Python-recursive functions

Source: Internet
Author: User

The definition of recursive recursion--the maximum depth--997 in a function that invokes the function itself recursively

How do you prove the theory of ' 997 '? Here we can do an experiment:

def foo (n):     Print (n)     + = 1    foo (n) foo (1)

From this we can see that the maximum number that can be seen before an error is 997. 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))

We can modify the maximum depth of recursion in this way, just as we set the recursive depth allowed by Python to 10w, and the actual depth that can be reached depends on the performance of the computer. However, it is still not recommended to modify this default recursion depth, because if the 997-layer recursion is not solved the problem is either not suitable for the use of recursive resolution or you write code is too bad ~ ~ ~

See here, you may feel that recursion is not how good things, as while true useful! However, the river has spread this sentence is called: people understand the cycle, God understand recursion. So don't underestimate the recursive function, many people have been stopped outside the threshold of the great God for so many years, because they failed to comprehend the true meaning of recursion. And then many of the algorithms we learn will have a relationship to recursion. Come, only learn to have capital to abandon!

Recursive instances

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?

Age (4) = Age (3) + 2 age (3) = Age (2) + 2 Age(2) = Age (1) + 2age (1) = 40

So, how do we write with a function?

def Age (N):     if n = =1        :return +    else:        return Age (n-1) +2 Print (age (4))
Recursive functions and level Three menu level three menu

defThreelm (DIC): whileTrue: forKinchDic:Print(k) Key= Input ('input>>'). Strip ()ifKey = ='b' orKey = ='Q':returnKeyelifKeyinchDic.keys () andDic[key]: ret=Threelm (Dic[key])ifRET = ='Q':return 'Q'Threelm (menu) Recursive function implementation level three menu
Three-level menu solution

 
L =[Menu] whileL: forKeyinchL[-1]:Print(key) K= Input ('input>>'). Strip ()#Beijing    ifKinchL[-1].keys () andL[-1][k]:l.append (l[-1][k])elifK = ='b': L.pop ()elifK = ='Q': BreakStack Implementation
Recursive SolutionsBinary search algorithm

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]
We can find this using the index method, because Python helps us find the way. If, the index method does not give you the use of ... Can you still find this 66?
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= 0 for in L:    if num = =:        print(i)    i+=1

The above method realizes the location of 66 from a list.

But how do we find this number now? is not the cycle of this list, one to find AH? If we have a very long list, and we have a good hundreds of thousands of-digit number, do we need to compare a hundred thousand of times if we have bad luck? It's so inefficient, we have to think of a new way.

Binary search algorithm
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]

Do you observe this list, which is not an ordered list of small to large sort?

If that's the case, if I'm looking for a larger number than the middle of the list, am I just going to look in the back half of the list?

This is the binary search algorithm !

Simple version of Binary method

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]deffunc (L,aim): Mid= (Len (l)-1)//2ifL:ifAim >L[mid]: func (L[mid+1:],aim)elifAim <L[mid]: func (L[:mid],aim)elifAim = =L[mid]:Print("Bingo", mid)Else:        Print('I can't find them .') func (L,66) func (L,6) The basic version of the dichotomy method
View Code

Two-way version of the upgrade

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 binary Algorithm Ultimate Edition
View Code

The path to Python-recursive functions

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.