高阶函数-递归

1.高阶函数

1.1高阶函数定义

变量可以指向函数,函数的参数能接受变量,那么一个函数就可以接收另一个函数作为参数,这种函数就称为高阶函数。
只要满足以下任意一个条件,即是高阶函数
1.接收一个或多个函数作为输入
2.return返回另外一个函数

#!/usr/bin/env python
# -*- coding:utf-8 -*-
# Author: vita

def add(x, y, func):
    return func(x) + func(y)
res = add(3, -6, abs)
print(res)

E:\PythonProject\python-test\venvP3\Scripts\python.exe E:/PythonProject/python-test/BasicGrammer/test.py
9

Process finished with exit code 0

2.递归

2.1递归定义

在函数内部,可以调用其它函数。如果一个函数在内部调用自身,这个函数就是递归函数。

#!/usr/bin/env python
# -*- coding:utf-8 -*-
# Author: vita

def calc(n):
    print(n)
    if int(n/2) == 0:
        return n
    return calc(int(n/2))
calc(4)

E:\PythonProject\python-test\venvP3\Scripts\python.exe E:/PythonProject/python-test/BasicGrammer/test.py
4
2
1

Process finished with exit code 0

2.2递归的执行过程

#!/usr/bin/env python
# -*- coding:utf-8 -*-
# Author: vita

def calc(n):
    print(n)
    if int(n/2) > 0:
        calc(int(n/2))
    print(n)
calc(4)

E:\PythonProject\python-test\venvP3\Scripts\python.exe E:/PythonProject/python-test/BasicGrammer/test.py
4
2
1
1
2
4

Process finished with exit code 0

```![](https://s1.51cto.com/images/blog/201905/01/c0982168aae8b7f5c44a1ffd214523f3.png?x-oss-process=image/watermark,size_16,text_QDUxQ1RP5Y2a5a6i,color_FFFFFF,t_100,g_se,x_10,y_10,shadow_90,type_ZmFuZ3poZW5naGVpdGk=)
  ## 2.3递归特性
    >1.必须有一个明确的结束条件
    >2.每次进入更深一层递归时,问题规模相比上次递归都应有所减少
    >3.递归效率不高,递归层次过多会导致栈溢出(在计算机中,函数调用时通过栈(stack)这种数据结构实现的,每当进入一个函数调用,栈就会加一层栈帧,每当函数返回,栈就会减一层栈帧。由于栈的大小不是无限的,所以递归调用的次数过多,会导致栈溢出)

    ## 2.4二分法查找
#!/usr/bin/env python

-- coding:utf-8 --

Author: vita

data = [1, 3, 5, 7, 8, 10, 30, 34, 35]

def binary_search(source_data, search_data):
print(source_data)
if len(source_data) > 1:
mid_data_index = int(len(source_data)/2)
if source_data[mid_data_index] == search_data:
print("excellent,you have found it!")
elif source_data[mid_data_index] > search_data:
source_data = source_data[0:mid_data_index]
binary_search(source_data, search_data)
elif source_data[mid_data_index] < search_data:
source_data = source_data[mid_data_index + 1:]
binary_search(source_data, search_data)
else:
if len(source_data) == 0:
print("source data is null now!")
else:
if source_data[0] == search_data:
print("you have found it!")
else:
print("there is not this number!")
binary_search(data, 30)

E:\PythonProject\python-test\venvP3\Scripts\python.exe E:/PythonProject/python-test/BasicGrammer/test.py
[1, 3, 5, 7, 8, 10, 30, 34, 35]
[10, 30, 34, 35]
[10, 30]
excellent,you have found it!

Process finished with exit code 0
"找4"
binary_search(data, 4)
E:\PythonProject\python-test\venvP3\Scripts\python.exe E:/PythonProject/python-test/BasicGrammer/test.py
[1, 3, 5, 7, 8, 10, 30, 34, 35]
[1, 3, 5, 7]
[1, 3]
[]
source data is null now!

Process finished with exit code 0

```

猜你喜欢

转载自blog.51cto.com/10983441/2388156