Python recursion and recursive functions

Source: Internet
Author: User

Recursive definition-- call the function itself in a function

Now we probably know what the story function has just been doing, which is to call the function itself in a function , and this magic way of using the function is called recursion .

We've just written one of the simplest recursive functions.

Max depth of recursion--997

As you have just seen, recursive functions are carried out if they are not blocked by external forces. But as we've said before about function calls, each time a function call produces a namespace that belongs to it, and if it keeps calling, it causes the namespace to take up too much memory, so Python is forced to control the recursive layer by 997 in order to eliminate this kind of phenomenon.

You can use a piece of code to verify the default recursion depth:

1 def foo (n): 2     Print (n) 3     n + = 14    foo (n)5 foo (1)

There are certain ways to modify the default recursion depth:

1 Import SYS 2 Print (Sys.setrecursionlimit (100000))

Recursive strength example explained:

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 Jin Xin 40

2 Wu Sir 42

3 Egon 44

4 Alex 46

The above process has been very close to the idea of recursion, and then the specific analysis of the rules between the several people.

Using a code representation is:

1 2 Age (3) = Age (2) + 23 age (2) = Age (1) + 24 age (1) = 40

Accordingly, the function should write:

1 def Age (n): 2     if n = = 1:3         return 4     else:5          return Age (n-1) +267print(age (4))

Thus, the nature of recursion and recursive functions can be obtained: the relationship between the two levels is continuously searched for known conditions (unknown contact known) to solve the unsolved problem by using known conditions.

Practical application of recursion: algorithm (binary search)

What should I do if I want to find the position of a number of simple, sorted numbers by size?

Using index (to find objects) is a common way to resolve them.

But for a list that has millions of ordered permutations, it can be very slow to use to calculate the result.

So we're going to find a way to solve such a problem------dichotomy (based on recursive functions)

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 take the middle number each time, according to the size of the middle number to determine the position of the number we are looking for (left or right)

So each time, you can keep approaching the number of places to take, and finally you can find the location of the number we are looking for.

This is the binary search algorithm !

So how do we implement it in the code?

Simple version:

1L = [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]2 3 deffunc (l,aim):4Mid = (len (l)-1)//25     ifL:6         ifAim >L[mid]:7Func (l[mid+1:],aim)8         elifAim <L[mid]:9 func (L[:mid],aim)Ten         elifAim = =L[mid]: One             Print("Bingo", mid) A     Else: -         Print('I can't find them .') -Func (l,66) theFunc (l,6)

High-end version:

1 defSearch (num,l,start=none,end=None):2Start = StartifStartElse03End = EndifEndElseLen (L)-14Mid = (End-start)//2 +Start5     ifStart >End:6         returnNone7     elifL[mid] >Num:8         returnSearch (num,l,start,mid-1)9     elifL[mid] <Num:Ten         returnSearch (num,l,mid+1, end) One     elifL[mid] = =Num: A         returnMid

---restore content ends---

Recursive definition-- call the function itself in a function

Now we probably know what the story function has just been doing, which is to call the function itself in a function , and this magic way of using the function is called recursion .

We've just written one of the simplest recursive functions.

Max depth of recursion--997

As you have just seen, recursive functions are carried out if they are not blocked by external forces. But as we've said before about function calls, each time a function call produces a namespace that belongs to it, and if it keeps calling, it causes the namespace to take up too much memory, so Python is forced to control the recursive layer by 997 in order to eliminate this kind of phenomenon.

You can use a piece of code to verify the default recursion depth:

1 def foo (n): 2     Print (n) 3     n + = 14    foo (n)5 foo (1)

There are certain ways to modify the default recursion depth:

1 Import SYS 2 Print (Sys.setrecursionlimit (100000))

Recursive strength example explained:

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 Jin Xin

2

3

4

Python recursion and 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.