2018-6-6-python full stack development day15-part2-recursion

Source: Internet
Author: User

Recursive

Continue running functions within the function, knowing that a goal is reached and the final return value.

Characteristics of recursion:

1. The function must have a definite end condition

2. Without entering a recursion, the problem that needs to be addressed should be reduced

3. Recursive efficiency is low, need to prevent memory overflow

Class Exercises

 

1. Ask the way

  

#Ask the way, there are several people on the road, ask if one of them knows where the place is.s1=['Alex','Xuzheng','Xiaoye','Haibin']defWenlu (S1): person=s1.pop (0)#the list of passers-by is paged out for questioning, and the list of non-questions is reduced    ifperson = ='Haibin':#only the seashore knows the way.        return 'Ah, I know how to get to the Taiping container .'    ifLen (S1) = =0:return 'no one knows where the road is.'    Print('I'm sorry, I don't know, I'll ask you.') Res=Wenlu (S1)returnResres=Wenlu (S1)Print(RES)

Very coarse recursion.

str = input (' Please enter several characters:')def  F (x):    if x = =-1:         return "'    Else :         return Str[x] + f (x-1)print(f (Len (str)-1)----Alex= = = Xela

Wrote it myself.

def Zhuan (x):     if x = = 0        :return'    else:        a=s[x-1]+zhuan (x-1 )        return  ab=Zhuan (len (s))print(b)

2018-6-6-python full stack development day15-part2-recursion

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.