Learning---Recursive function of Python learning

Source: Internet
Author: User

definition: inside a function, other functions can be called. If a function calls itself internally, the function is a recursive function.

Recursive properties:

1. There must be a clear end condition

2. Each time a deeper level of recursion is reached, the problem size should be reduced compared to the previous recursion

3. Recursive efficiency is not high, too many recursive hierarchy will lead to stack overflow (in the computer, function calls through the stack (stack) This data structure implementation, each time into a function call, the stack will add a stack of frames, whenever the function returns, the stack will reduce the stack frame. Because the size of the stack is not infinite, there are too many recursive calls that can cause the stack to overflow. )

Example 1: (factorial)

# normal method def factorial (num):    FAC = num    for i in range (1, num):        FAC *= i    return Facprint (factorial (4)) # Recursive implementation de f factorial (num):   if num = = 1:                     # End Condition        return 1   return factorial (num-1) * Numprint (factorial (4)) # Functional Programming Implementation : From Functools Import reduceprint (reduce (lambda x,y:x*y, Range (1,5))  # 24

Example 2: Fibonacci sequence

# Common implementations: # 0 1 1 2 3 5 8 13def Fibo (n):    before = 0 after    = 1    ret = 0 for    i in range (n-1):        ret = Before + after        before = after after        = ret    return Retprint (Fibo (6))                 # 8# Recursive implementation: # 0 1 1 2 3 5 8 13def fibo_new (n): c9/># n can be zero, the sequence has [0]    if n <= 1:            # End Condition   F (0) =0,f (1) =1,f (2) =f (1) +f (0)--meet return criteria, so <=1        return n    return (fibo_new (n-1) + fibo_new (n-2))   # f (8) = f (7) + F (6) Print (Fibo_new (6))                  #  8# other implementations de F fib (max):    N, b, a = 0, 0, 1    while n < max:        print (b)        B, a = A, B+a  # Assignment is        n + = 1fib ( 6)   # 8

Example 3: Number =[2,-5, 9,-7, 2, 5, 4,-1, 0,-3, 8] average of positive numbers in

# recursive Implementation number = [2, -5, 9,-7, 2, 5, 4,-1, 0, -3, 8]count = 0li = []for i in range (len (number)    ]: if number[i] > 0:< C1/>li.append (Number[i])        count + = 1count = str (count) print ("%s of positive integers and%d"% (count, sum (LI))
# Functional Programming

# to write

Learning---Recursive function of Python learning

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.