Recursive function and higher-order function

Recursive function and higher-order function

#_author: Administrator 
#date: 2019/11/1
#. 1

DEF Fun (* args):
Print (args)
A = [l, 2,3]
Fun (A *) # (. 1, 2,. 3)
Print ( ' -------------- ')
DEF fun1 (** kwargs):
Print (kwargs)
fun1 (** {' name ':' Star '}) {#' name ':' Star '} pass directly a dictionary
# 2. Higher order functions
# (1) function name is a variable can be assigned
# (2) can be used as parameters of the function name of the function, may also function as a return value
def f (n) :
return * n-n-
DEF fun2 (A, B, FUNC):
return FUNC (A) + FUNC (B)
Print (fun2 (2,3, F)) # 13 is
Print ( '--------- ----------------------- ')
DEF FU ():
DEF F ():
return. 5
return F
m = FU ()
Print (m) # <function FU <about locals> .F AT 0x03719738.>Is a function object address
print (m ()) # 5
Print ( '---------------------')
# recursive function. 3
DEF Jie (n-):
IF n-==. 1:
return. 1
return n-Jie * (n- -1)
Print (Jie (. 5)) # 120
Print ( '---------------------')
# characteristics with respect to recursive:
# 1 internal call itself function
# 2 has a termination condition
# recursion can solve the problems can be solved with a loop
# recursion in many cases inefficient
# Fibonacci column
DEF fibo (the n-):
IF the n-<= 2:
return the n-
return fibo (. 1-n-) FIBO + (2-n-)
Print (FIBO (. 8)) # 34 is
# 2. 3. 1. 5. 1 0. 8 13 is 21 is 34 is 55
Print ( '-------------- ------- ')

DEF fibo1 (n-):
IF or n-n-== == 0. 1:
return n-
return fibo1 (. 1-n-) fibo1 + (2-n-)
Print (fibo1 (. 8)) # 21
# 4. built-in functions
print ( '---------------------')
print (all ([1,2,3, ' asd'])) # True all () function is used for all elements in a given determination may be whether the iteration parameters iterable are TRUE, a return True, otherwise return False .
Print (All ([l, 2,3, 'ASD', ''])) # False
Print (divmod (8,3)) # remainder number and the die (2, 2)
Print (the eval ( '+. 4. 3 * 6 ')) # 27 eval ( ) can help us to do arithmetic, but also can be converted to a string dictionary
# important of built-in functions (1) filter function is a function element in the sequence does not meet the criteria to filter out, when the sequence when you want to cut some of the elements can function description, it should be remembered filter function.
STR = [ 'A', 'S', 'D', 'F']
DEF FUNC (S):
IF S = 'D':!
return S
RET = filter (FUNC, STR)
Print (RET) # <filter object at 0x007809F0> ret is a filter object, the object is an iterator
Print (List (RET)) # [ 'a', 'S', 'F']
Print ( '---------- -------------- '
) # (2) Map () is a function of higher order built Python, it receives a list and a function f, the function f and sequentially by acting on each element of the list to obtain a new list and returns.
= List [ 'A', 'S', 'D', 'F']
DEF FUNC (S):

return S + 'Star'
RET2 = Map (FUNC, List)
Print (RET2) # <Map Object AT 0x03910B10>
Print (List (RET2)) # [ 'Astar', 'SStar', 'DSTAR', 'fstar'] of string process
# (3) reduce () in order to invoke this method, the reduce preceded Import functools from
# sequence to a compression algorithm to obtain a value. But reduce the time python2 is built-in functions, to python3 moved functools module, so it is necessary from functools import reduce Before using the
# call: reduce (function, iterable), which must function with two arguments, iterable may be a list or yuan group

from the reduce functools Import
DEF ADD l (X, Y):
return X + Y
Print (the reduce (ADD l, [1,2,3,4,5,6,7,8,9])) # 45 the reduce () of The result is a value
Print (the reduce (ADD l, Range (1,101))) # 5050
# (. 4) the lambda -> anonymous function
DEF add_2 (a, B):
return a + B

the lambda X, Y: X + Y
# by reduce function plus lambda expressions (anonymous function) factorial
Import the reduce functools from
Print (the reduce ((lambda A, B: A * B), Range (l, 5))) # 24
# lambda expression added by the map function (anonymous function)
Squares = map (lambda X: X * X, Range (. 9))
Print (Squares) # <Map Object AT 0x01250D90>
Print (List (Squares)) # [0,. 1,. 4,. 9, 16, 25, 36, 49, 64]



Guess you like

Origin www.cnblogs.com/startl/p/11781274.html