Python Learning week5-recursion, anonymous function, generator

Source: Internet
Author: User

1. Recursive function

1.1. Limitations of Recursive functions

(1) Recursive must have exit conditions, and recursive call must be executed to this exit condition, if there is no exit condition, is infinite call, will exhaust all resources (stack space);

(2) The depth of recursive calls is not too deep, Python limits the depth of recursive calls to protect the interpreter;

1.2. Recursive instances

①, recursive implementation of the thin-cut sequence

#Version1: Mathematical formula versiondeffib (n):return1ifN<3ElseFIB (n-1) +fib (n-2)Print(FIB (10) Analysis: Although this version looks concise and clear, but there will be a lot of efficiency problems, when the N operation 35 after the calculation results are very slow#Version2: Using the last resultdefFIB (n,a=0,b=1):#n=3    ifN<3:        returnA +bElse: A, b=b,a+b#0,1=1,1        returnFIB (n-1, A, b) analysis: The version uses the result of the last operation every time, so the efficiency is much faster than the first version;#for loop Implementationa,b=0,1 forIinchRange (10): A, B=b,a+bPrint(a) Analysis: Version Two is based on the For Loop Evolution

②, recursive implementation of factorial

def FAC (n,sum=1):    if n==1:        return 1    return N*FAC (n-1) def FAC (n,sum=1):    sum=sum*n    if n<2:         return sum     return FAC (n-1, sum)#  version Two brings the results of each calculation into the next calculation

③, put a number in reverse order into the list


nums='1234'Print(Nums[len (nums)-1]) L=[] forIinchRange (len (nums) -1,-1,-1): L.append (Nums[i])Print(L)
# for Loop implementation
defrevert (x):ifx = =-1: return [] return[Nums[x]] + revert (x-1)Print(Revert (Len (nums)-1))
# List Stitching ImplementationdefRevert (x,target=[]): ifX:target.append (x[-1]) revert (x[:-1]) returnTargetPrint(Revert (Nums))
# string Slicing Real

④ and recursion to solve the problem of monkey eating peach

" "Xd1=x//2-1D2=d1//2-1D3=d2//2-1... D9=d8//2-1 ==> 2 (d9+1) =d8=d7/2-1" "peach=1for i in range (9): peach=2* (peach+1) print (peach)" "the problem has to be pushed backwards. The 10th day has not eaten, left 1, the 9th day ate half eat 1 still left 1, assuming that the 9th day has not eaten before there is a peach p, can get: P* 1/2-1 = 1, available p = 4. And so on, you can calculate the hand. The code idea is: the 10th day has not eaten before the peach number initialization P= 1, followed from 9 to 1 cycles 9 times, according to the above formula to push back to P = (p+1) * 2The number of peaches before the 1th day is not eaten.
The print () statement in the For loop is added to validate the extrapolation process. The code is as follows:P= 1Print('There are 1 peaches left before the 10th day of eating .') forIinchRange (9, 0, 1): P= (p+1) * 2Print('%s a peach before the first day of eating'%(i, p))Print('A total of%s Peaches were picked up on the 1th day.'%p)" "# Recursive Implementation def Peach (n=10): if n = = 1:return 1 return (Peach (n-1) +1) *2print (Peach ())
2. Anonymous function 3, generator 4, insert sort

Python Learning week5-recursion, anonymous function, generator

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.