Python Basics-fifth-5.2 recursion

Source: Internet
Author: User

Is another sunny day, small white look at just from the East Rising Sun, feel the Sun Grandpa also greet her, small white on the smile up! Thought: What interesting things will you learn today? Some small expectations, but also some small excitement!

Xiao Liu came earlier than the small white, two laugh played a greeting!

Xiao Liu: Xiao Bai! You come over here and let you think about how this code is executed? Why don't you just pick up the morning and wake up?

Xiao Liu: I want to go to the early meeting! You think about it and tell me when I get back!

Small white point nod, small bai look this piece of code:

Def f1 ():    return ' F1 ' def f2 ():    r = F1 ()    return Rdef F3 ():    r = F2 ()    return Rren = F3 ()

Small white put this picture of the colorful, small white point head, Mouth said a little meaning

Xiao Liu came back and asked her how she was thinking.

Small White replied: The result of the Ren variable is ' F1 ', explained from the top down to explain, when the definition function does not execute, just brush to memory, execute F3 up and down ran a back and forth

Xiao Liu looked at a little white painting of the picture, instantly understand, hurriedly praised small white: basic science is good ah, yes, you said

Xiao Liu: Then I'll show you an example, see if you can realize it?

def f4 (A1,A2):    If A1 > 3:        return    print (a1)    a3 = A1 + A2    F4 (a2,a3) R = F4 (0,1) print (R)

Small white looked, and drew up again

  

Little White will come out quickly. The 0,1,1,2,3 function returns the default value of None

Xiao Liu laughs: little white! You go back! No, I'll teach you.

Small white smiled: stupid people have stupid way, good time long, still need you this big God guidance!

Xiao Liu asked: "Do you know what this algorithm is called?"

Small white: It seems to be recursive, have learned a little before

Xiao Liu: Do you know the advantages and disadvantages of it and its definition?

Little White shook his head!

Xiao Liu: The Little White Mind

1. Calling itself in a procedure or function

2. Recursive algorithm is simple and easy to read, but inefficient

3. When using a recursive strategy, there must be an end condition, which is called a recursive exit with a maximum recursion depth of 999 layers

Small white exclamation way: a little one recursion so much learning AH

Xiao Liu: Don't you know that the series you just came up with is called the Fibonacci sequence?

Little white: I don't know.

Xiao Liu: That is, a number equals the first two, and I have a function to set the recursion depth to you here.

def f5 (DEPTH,A1,A2):    if depth = = Ten:        return a1    a3 = A1 + A2    r = F5 (DEPTH+1,A2,A3)    return rret = F5 (1,0 , 1) print (ret)

Xiao Liu: Xiao Bai! Does this make sense?

Little white dot nod

Xiao Liu: So let's look at a more complicated example, called binary search, which uses the principle of recursion.

Binary Method Search

Binary search is also called semi-search, the premise is to find the ordered series, take the number to find and in the middle of the data source to compare, every comparison to exclude half

#二分法查找lis = List (range (2,11,4)) print (LIS) #[2, 6, 10]def Semi_find (find_n,data_resources):    i = int (len (data_ Resources)/2)  #求得中间项的索引位置    If Len (data_resources) >= 1:    #递归终止条件        if data_resources[i] > find_n:  #要查找的数处在前半部分            semi_find (find_n,data_resources[:i])  #排除后半部分        elif data_resources[i] < find_n:  #要查找的数处在后半部分            If find_n < Data_resources[i + 1]:  #因为int对小数是向下取整, this excludes the value between the corresponding number of index positions I to i+1                print (' That's not the count! ')               #一旦要查找的数判定处于上面范围, that is, the output does not have this number                return            else:                semi_find (find_n,data_resources[i:])  # Excluding the first half of the        else:            print ('%s in Lis '%find_n)  #如果等于中间数, we found the    else:        print (' No!! ') #加等号semi_find (2,lis)  #能找到2semi_find (7,lis)  #7, 8,9 more than the maximum number of recursive layers also resolves # no equals # Semi_find (2,lis)  #错误提示找不到2 # Semi_find (7,lis)  #能正确提示7, 8,9 does not exist

You are welcome to ask questions and questions about my blog content! Thanks

Author: Pat Province Mr.

  

Python Basics-fifth-5.2 recursion

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.