python recursion (function)

Recursion: a procedure or function calls itself a way.

FIG. 1. Effect

2. Code

DEF factorial (n):
     '' ' 
        of the factorial function for any number of 

        parameters: 
            n factorial required numbers 
    '' 
    # baseline condition is determined whether n is 1, if a case is not continue recursively 
    IF n = = 1 :
         # factorial 1 1 is directly returned 1 
        return 1 # recursive conditions     return n-factorial * (n-1- ) Print (factorial (. 5))

    
    

Original notes:

# Recursion is one way to solve the problem, it is like cycling and 
#    its overall idea is a big problem into one small problem, until the problem can not break down, go solve the problem 
# recursive function of two a elements 
#    1. baseline condition 
#        - problem can be broken down into the smallest problem, when the baseline condition satisfied, recursion is not performed in the 
#    2 condition recursive 
#        - will continue to issue an exploded condition 
# similar recursive cycle and, substantially can replace each other, 
#    cycle is easier to write, read up a little difficult 
#    recursively to write and difficult, but easy to read 


# practice 
#    create a function power to do exponentiation n ** i is any number 
#    10 * 5 = 10 . 4 ** 10 * 
#    10 ** 10 * 10 **. 4. 3 = 
#    ... 
#    10 ** = 10. 1 
DEF Power (n-, I):
     '' '
        power () is used to make any number exponentiation 

        parameters: 
            n-do exponentiation digital 
            times i do exponentiation 
    '' ' 
    # baseline condition 
    IF i == 1 :
         # find a power 
        return n-
     # recursive conditions 
    return * Power the n-(the n-, i-1 ) 

# Print (Power (8, 6))     



#    
# exercise 
#    create a function that checks whether an arbitrary string is a palindrome string, a return True, otherwise return False 
#    palindrome string, the string read from front to back and forward to read from the same 
#        ABCBA 
#    abcdefgfedcba 
#    to check whether the first character and the last character, and if not then the string is not a palindrome 
#        if consistent, to see if the remaining part of the palindrome string 
#   Check abcdefgfedcba is not a palindrome 
#    check bcdefgfedcb is not a palindrome 
#    check cdefgfedc is not a palindrome 
#    check defgfed is not a palindrome 
#    check efgfe is not a palindrome 
#    check fgf is not a palindrome 
#    check g is not a palindrome 

DEF hui_wen (S):
     '' ' 
        this function checks whether the specified string palindrome string, a return True, otherwise it returns False 

        parameters: 
            S: is to check the string 
    ' '' 
    # baseline conditions 
    IF len (S ) <2 :
         # length of the string is less than 2, then the string must palindromic 
        return True
     elif S [0] = S [-1! ]:
         # first character and the last character are not equal, the character is not a palindrome string 
        returnFalse    
     # recursive conditions     
    return hui_wen (S [. 1: -1 ]) 

# DEF hui_wen (S): 
#      '' ' 
#          This function is used to check whether the specified string palindrome string, a return True, otherwise return False 

#          parameters: 
#              S: is the string to check 
#      '' ' 
#      # baseline conditions 
#      IF len (S) <2: 
#          length # 2 is less than the string, the string must palindromic 
#          return True 
#      # recursive condition     
#      return S [0] == S [-1] and hui_wen (S [. 1: -1]) 

Print (hui_wen ( ' abcdefgfedcba ' ))

 

Guess you like

Origin www.cnblogs.com/FlyingLiao/p/11279913.html