recursive recursive python python

python recursion

 

1.1 Recursive explain

  1. Definitions

      1. Inside the function, you can call other functions. If a function calls itself within itself, this function is a recursive function.

  2, the recursive nature

      1. There must be a definite end condition

      2. Each entry recursive deeper, the scale of the problem should be reduced compared to the previous recursive

      3. recursive efficiency is not high, too many levels of recursion can cause a stack overflow (in the computer, the function call is achieved through the stack (stack) this data structure, whenever entering a function call,

          Stack would add a layer stack frames, each time the function returns, the stack will be reduced one stack frame. Due to the size of the stack is not infinite, so the number of recursive calls too much will result in a stack overflow)

1.2 simple example to understand the principle of recursive

    Reference blog: https: //www.cnblogs.com/Fantinai/p/7806356.html

  1, recursive instances

  Recursive case
! # / Usr / bin / Python the env 
# - * - Coding: UTF-. 8 - * - 
DEF digui (NUM): 
    Print NUM 
    IF NUM> 0: 
        digui (NUM -. 1) 
    the else: 
        Print '------ ------ ' 
    Print NUM 

digui (. 3) 

' '' execution result 
. 3 
2 
. 1 
0 
------------ 
0 
. 1 
2 
. 3 
'' '

      1. Every time there is a return of the function calls, and one is a return to the calling recursively that one, instead of returning directly to the main (in the initial part of the function call). 

      2. The first recursion: n = 3 3 [3] Drawing

      3. The second recursion: n = 2 2 Drawing [3, 2]

      4. Third recursion: n = 1 1 Drawing [3,2,1]

      5. When n = 0, 0> 0 is False, not recursive, print num = 0, the function returns to the caller on his level, i.e., n = 1 stack

      6. Then the position digui (num - 1) performs a downward: A print print num = 1, 1 the stack, the stack elements: [3,2]

      7. 2, 3, and so it will print the final print at right

                

  2, analysis results

      1. Why would the outcome of the above it? Because regarded calling code after the function itself to forget, is the python code after else.

      2. When calling the function itself, the code does not end after it, but in the waiting condition is False, then again after execution of the code, the same color print () function statement waits for the corresponding color.

      3. Let me put this to do a recursive function decomposition, Detailed recursive function, when you call a recursive function digui (3), the implementation process is as follows:

      

 1.2 recursive factorial resolve recursive principle

  1, the code factorial

  Code 4 factorial
#! /usr/bin/env python
# -*- coding: utf-8 -*-
def test(n):
    if n == 1:
        return 1
    else:
        res = n*test(n-1)
        print "n:%s-----ret:%s"%(n, res)
    return res

print test(4)  # 24
'''
n:2-----ret:2
n:3-----ret:6
n:4-----ret:24
24
'''
  Recursive factorial deduction of 4
# 1, recursive step 
'' ' 
1, a first layer: Test (. 4) = * Test. 4 (4-1) 
2, a second layer: Test (. 3) * Test. 3 = (3-1) 
. 3, the third layer: Test (2) Test = 2 * (2-1) 
. 4, the fourth layer: Test (. 1) =. 1 
'' ' 

# 2, returns to step 
' '' 
Note: Über position is: res = n * test (n-1), it will then return to the upper rear downward known herein performs return 
. 5, n-=. 1 will then execute the code in the code block return 1 if the end of this time the fourth layer functions: RET =. 1 
. 6, after completion of the fourth layer position of the third function is then performed down to the layer calls return: RET =. 1 * 2 
. 7, the third layer will return back to the calling function after the return position of the second layer: ret = 1 * 2 * 3 
8, the second layer will return back to the calling function the position of the return of the first layer: ret = 1 * 2 * 3 * 4 
after the call reaches the position of the first layer, the upper position not used recursively, this time only the function returns true positive . 
'' '

    

1.3 frog jump step problems 

    Reference blog: https://cloud.tencent.com/developer/news/44122

  1, two step problem

      Problem: A frog can jump on a Class 1 level, you can also hop on level 2. The frog jumped seeking a total of n grade level how many jumps 

  Two step (recursive function )
! # / Usr / bin / Python the env 
# - * - Coding: UTF-. 8 - * - 
Import SYS 
sys.setrecursionlimit (1000000000) # Set the system maximum depth of recursion 

DEF FIB (n-): 
    IF n-<= 2: 
        return n- 
    the else : 
        return FIB (. 1-n-) + FIB (2-n-) 
Print (FIB (. 4)). 5 #
  Two step deduction
#### 1, n = 1 only when A method 
# f (1) = 1 

When #### 2, n = 2 when a first step jump, there is a method, when the first two hops when a process step has a 
# f (2) =. 1. 1 + 2 = 

####. 3, the final pushed down. 3 = n-jumping step has a f (n-1) a method jumping last two steps f ( 2-n-) 
# F (. 3) = F (2) + F (. 1). 3 = 

####. 4, n-> 2 so 
# f (n) = f ( n-1) + f (n- 2)

  2, n stairs problem

      Problem: A frog can jump on a Class 1 level, you can also hop on level 2 ...... n It can also jump on stage. The frog jumped seeking a total of n grade level how many jumps

  Problems of steps n (recursive function)
! # / Usr / bin / Python the env 
# - * - Coding: UTF-. 8 - * - 
Import SYS 
sys.setrecursionlimit (1000000000) # Set the system maximum depth of recursion 

DEF FIB (n-): 
    IF n-<= 2: 
        return n- 
    the else : 
        return FIB 2 * (n--. 1) 
Print (FIB (. 4)). 8 #
  n stairs deduction
#### 1, n = 1 only when A method 
# f (1) = 1 

When #### 2, n = 2 when a first step jump, there is a method, when the first two hops one method when a step 
# F (n-) =. 1. 1 + 2 = 

#### 3, when the first hop = n-3 has a level f (3-1) in the method, when the first hop there f (3-2) when the two method steps, there are f (3-3) when the seed jumps first hop three steps 
# F (n-) =. 1 + 2. 3 = 

####. 4, n> 2 and so on 
# F (n-) = F (n--. 1) + F (n--2) + ...... F (0) 

'' ' 
F (n-) = F (n--. 1) + f (n-2) + ...... f (0) species jumps 

f (n-1) = f (n-2) + f (n-3) + ..... f (0 ) 

f (n-) -f (n--. 1) = f (n--. 1) 

so that f (

  3, issue three steps

  Question three steps
! # / Usr / bin / Python the env 
# - * - Coding: UTF-. 8 - * - 
Import SYS 
sys.setrecursionlimit (1000000000) # Set the system maximum depth of recursion 

DEF FIB (n-): 
    IF n-<= 2: 
        return n- 
    elif ==. 3 n-: 
        return. 4 
    the else: 
        return FIB (. 1-n-) + FIB (-n-2) + FIB (. 3-n-) 
Print (FIB (. 4)). 7 #

1.1 Recursive explain

  1. Definitions

      1. Inside the function, you can call other functions. If a function calls itself within itself, this function is a recursive function.

  2, the recursive nature

      1. There must be a definite end condition

      2. Each entry recursive deeper, the scale of the problem should be reduced compared to the previous recursive

      3. recursive efficiency is not high, too many levels of recursion can cause a stack overflow (in the computer, the function call is achieved through the stack (stack) this data structure, whenever entering a function call,

          Stack would add a layer stack frames, each time the function returns, the stack will be reduced one stack frame. Due to the size of the stack is not infinite, so the number of recursive calls too much will result in a stack overflow)

1.2 simple example to understand the principle of recursive

    Reference blog: https: //www.cnblogs.com/Fantinai/p/7806356.html

  1, recursive instances

  Recursive case
! # / Usr / bin / Python the env 
# - * - Coding: UTF-. 8 - * - 
DEF digui (NUM): 
    Print NUM 
    IF NUM> 0: 
        digui (NUM -. 1) 
    the else: 
        Print '------ ------ ' 
    Print NUM 

digui (. 3) 

' '' execution result 
. 3 
2 
. 1 
0 
------------ 
0 
. 1 
2 
. 3 
'' '

      1. Every time there is a return of the function calls, and one is a return to the calling recursively that one, instead of returning directly to the main (in the initial part of the function call). 

      2. The first recursion: n = 3 3 [3] Drawing

      3. The second recursion: n = 2 2 Drawing [3, 2]

      4. Third recursion: n = 1 1 Drawing [3,2,1]

      5. When n = 0, 0> 0 is False, not recursive, print num = 0, the function returns to the caller on his level, i.e., n = 1 stack

      6. Then the position digui (num - 1) performs a downward: A print print num = 1, 1 the stack, the stack elements: [3,2]

      7. 2, 3, and so it will print the final print at right

                

  2, analysis results

      1. Why would the outcome of the above it? Because regarded calling code after the function itself to forget, is the python code after else.

      2. When calling the function itself, the code does not end after it, but in the waiting condition is False, then again after execution of the code, the same color print () function statement waits for the corresponding color.

      3. Let me put this to do a recursive function decomposition, Detailed recursive function, when you call a recursive function digui (3), the implementation process is as follows:

      

 1.2 recursive factorial resolve recursive principle

  1, the code factorial

  Code 4 factorial
#! /usr/bin/env python
# -*- coding: utf-8 -*-
def test(n):
    if n == 1:
        return 1
    else:
        res = n*test(n-1)
        print "n:%s-----ret:%s"%(n, res)
    return res

print test(4)  # 24
'''
n:2-----ret:2
n:3-----ret:6
n:4-----ret:24
24
'''
  Recursive factorial deduction of 4
# 1, recursive step 
'' ' 
1, a first layer: Test (. 4) = * Test. 4 (4-1) 
2, a second layer: Test (. 3) * Test. 3 = (3-1) 
. 3, the third layer: Test (2) Test = 2 * (2-1) 
. 4, the fourth layer: Test (. 1) =. 1 
'' ' 

# 2, returns to step 
' '' 
Note: Über position is: res = n * test (n-1), it will then return to the upper rear downward known herein performs return 
. 5, n-=. 1 will then execute the code in the code block return 1 if the end of this time the fourth layer functions: RET =. 1 
. 6, after completion of the fourth layer position of the third function is then performed down to the layer calls return: RET =. 1 * 2 
. 7, the third layer will return back to the calling function after the return position of the second layer: ret = 1 * 2 * 3 
8, the second layer will return back to the calling function the position of the return of the first layer: ret = 1 * 2 * 3 * 4 
after the call reaches the position of the first layer, the upper position not used recursively, this time only the function returns true positive . 
'' '

    

1.3 frog jump step problems 

    Reference blog: https://cloud.tencent.com/developer/news/44122

  1, two step problem

      Problem: A frog can jump on a Class 1 level, you can also hop on level 2. The frog jumped seeking a total of n grade level how many jumps 

  Two step (recursive function )
! # / Usr / bin / Python the env 
# - * - Coding: UTF-. 8 - * - 
Import SYS 
sys.setrecursionlimit (1000000000) # Set the system maximum depth of recursion 

DEF FIB (n-): 
    IF n-<= 2: 
        return n- 
    the else : 
        return FIB (. 1-n-) + FIB (2-n-) 
Print (FIB (. 4)). 5 #
  Two step deduction
#### 1, n = 1 only when A method 
# f (1) = 1 

When #### 2, n = 2 when a first step jump, there is a method, when the first two hops when a process step has a 
# f (2) =. 1. 1 + 2 = 

####. 3, the final pushed down. 3 = n-jumping step has a f (n-1) a method jumping last two steps f ( 2-n-) 
# F (. 3) = F (2) + F (. 1). 3 = 

####. 4, n-> 2 so 
# f (n) = f ( n-1) + f (n- 2)

  2, n stairs problem

      Problem: A frog can jump on a Class 1 level, you can also hop on level 2 ...... n It can also jump on stage. The frog jumped seeking a total of n grade level how many jumps

  Problems of steps n (recursive function)
! # / Usr / bin / Python the env 
# - * - Coding: UTF-. 8 - * - 
Import SYS 
sys.setrecursionlimit (1000000000) # Set the system maximum depth of recursion 

DEF FIB (n-): 
    IF n-<= 2: 
        return n- 
    the else : 
        return FIB 2 * (n--. 1) 
Print (FIB (. 4)). 8 #
  n stairs deduction
#### 1, n = 1 only when A method 
# f (1) = 1 

When #### 2, n = 2 when a first step jump, there is a method, when the first two hops one method when a step 
# F (n-) =. 1. 1 + 2 = 

#### 3, when the first hop = n-3 has a level f (3-1) in the method, when the first hop there f (3-2) when the two method steps, there are f (3-3) when the seed jumps first hop three steps 
# F (n-) =. 1 + 2. 3 = 

####. 4, n> 2 and so on 
# F (n-) = F (n--. 1) + F (n--2) + ...... F (0) 

'' ' 
F (n-) = F (n--. 1) + f (n-2) + ...... f (0) species jumps 

f (n-1) = f (n-2) + f (n-3) + ..... f (0 ) 

f (n-) -f (n--. 1) = f (n--. 1) 

so that f (

  3, issue three steps

  Question three steps
! # / Usr / bin / Python the env 
# - * - Coding: UTF-. 8 - * - 
Import SYS 
sys.setrecursionlimit (1000000000) # Set the system maximum depth of recursion 

DEF FIB (n-): 
    IF n-<= 2: 
        return n- 
    elif ==. 3 n-: 
        return. 4 
    the else: 
        return FIB (. 1-n-) + FIB (-n-2) + FIB (. 3-n-) 
Print (FIB (. 4)). 7 #

Guess you like

Origin www.cnblogs.com/bokeyuanan/p/12549186.html