python - - 函数 - - 递归函数

目录

  • 什么是递归
  • 递归例题
  • 递归函数与二分查找算法
  • 相关练习题

1,什么是递归

  • 递归的定义:在一个函数里再调用这个函数本身
  • 递归的最大深度默认是:997 - - 是python从内存角度出发做的限制
  • 递归的缺点: 占内存
  • 递归的优点: 会让代码变简单

1.1 测试递归最大深度

n = 0
def recursion():
    global n
    n += 1
    print(n)
    recursion()

recursion()

# 结果呈现
1
2
...
995
996
Traceback (most recent call last):
  File "C:/Users/thinkpad/Envs/daily_test/test_project/递归函数.py", line 27, in <module>
    recursion()
  File "C:/Users/thinkpad/Envs/daily_test/test_project/递归函数.py", line 25, in recursion
    recursion()
  File "C:/Users/thinkpad/Envs/daily_test/test_project/递归函数.py", line 25, in recursion
    recursion()
  File "C:/Users/thinkpad/Envs/daily_test/test_project/递归函数.py", line 25, in recursion
    recursion()
  [Previous line repeated 992 more times]
  File "C:/Users/thinkpad/Envs/daily_test/test_project/递归函数.py", line 24, in recursion
    print(n)
RecursionError: maximum recursion depth exceeded while calling a Python object
RecursionError: maximum recursion depth exceeded while calling a Python object
递归的错误:超过的递归的最大深度

1.2 修改递归最大深度

import sys
sys.setrecursionlimit(100000)

n = 0
def recursion():
    global n
    n += 1
    print(n)
    recursion()

recursion()

# 结果呈现
1
2
...
3924
3925

2,递归例题

2.1 A 比 B 大两岁,B 比 C 大两岁,C 比 D 大两岁,D 比 F 大两岁,F 18岁了,求A多大了?

  • 分析规律
age(A) = age(B) + 2
age(B) = age(C) + 2
age(C) = age(D) + 2
age(D) = age(F) + 2
age(F) = 18
  • 函数如下
def age(n):
    if n == 5:
        return 18
    elif n > 0 and n < 5:
        return age(n+1) +2

print(age(1))

# 或
def age(n):
    if n == 1:
        return 18
    else:
        return age(n-1)+2

print(age(5))

# 结果呈现
26

3,递归函数与二分查找算法

3.1 例题

  • 如果有这样一个列表,让你从这个列表中找到66的位置,你要怎么做?
l = [2,3,5,10,15,16,18,22,26,30,32,35,41,42,43,55,56,66,67,69,72,76,82,83,88]
print(l.index(66))

# 结果呈现
17
  • 不用 index,如何查找
l = [2,3,5,10,15,16,18,22,26,30,32,35,41,42,43,55,56,66,67,69,72,76,82,83,88]
i = 0
for num in l:
    if num == 66:
        print(i)
    i+=1

# 结果呈现
17

3.2 二分查找算法

  • 二分查找算法,必须处理有序的列表

3.2.1 有一个列表,从这个列表中找到66的位置

l = [2,3,5,10,15,16,18,22,26,30,32,35,41,42,43,55,56,66,67,69,72,76,82,83,88]

通过 index 方法获取

l = [2,3,5,10,15,16,18,22,26,30,32,35,41,42,43,55,56,66,67,69,72,76,82,83,88]
print(l.index(66))

# 结果呈现
17

通过 for 循环获取

l = [2,3,5,10,15,16,18,22,26,30,32,35,41,42,43,55,56,66,67,69,72,76,82,83,88]

i = 0
for num in l:
    if num == 66:
        print(i)
    i+=1

# 结果呈现
17

通过 二分查找算法 获取

l = [2,3,5,10,15,16,18,22,26,30,32,35,41,42,43,55,56,66,67,69,72,76,82,83,88]
def find(l, aim, start = 0, end=None):
    end = len(l) if end is None else end
    mid_index = (end - start) // 2 + start  # 计算中间值
    if start <= end:
        if l[mid_index] > aim:
            return find(l, aim, start = start, end= mid_index - 1)
        elif l[mid_index] < aim:
            return find(l, aim, start = mid_index + 1, end=end)
        else:
            return "找到了",mid_index, aim
    else:
        return "找不到这个值", aim


ret = find(l, 66)
ret1 = find(l, 44)
print(ret)
print(ret1)

# 结果呈现
('找到了', 17, 66)
('找不到这个值', 44)

4 , 相关练习题

4.1 斐波那契 # 问第N个斐波那契数是多少

# 1, 1, 2, 3, 5, 8
# fib(6) = fib(5) + fib(4)
# fib(5) = fib(4) + fib(3)
# fib(4) = fib(3) + fib(2)
# fib(3) = fib(2) + fib(1)
# fib(2) = 1
# fib(1) = 1
  • 使用递归 调用自己两次的方法,效率慢,占内存
def fib(n):
    if n == 1 or n ==2:
        return 1
    return fib(n - 1) + fib(n - 2)

ret = fib(6)
print(ret)

# 结果呈现
8
  • 使用递归 调用自己一次的方法
def fib(n, lst = [0]):
    lst[0] += 1
    if n == 1 or n == 2:
        lst[0] -= 1
        return 1, 1
    else:
        a, b = fib(n - 1)
        lst[0] -= 1
        if lst[0] == 0:
            return a+b
        return b, a+b

print(fib(6))

# 结果呈现
8

4.2 阶乘

def fac(n):
    if n == 1:
        return 1
    return n * fac(n - 1)

print(fac(6))

# 结果呈现
# 6!  6 * 5 * 4 * 3 * 2 * 1
720

4.3 递归函数与三级菜单

  • 三级菜单
menu = {
    '北京': {
        '海淀': {
            '五道口': {
                'soho': {},
                '网易': {},
                'google': {}
            },
            '中关村': {
                '爱奇艺': {},
                '汽车之家': {},
                'youku': {},
            },
            '上地': {
                '百度': {},
            },
        },
        '昌平': {
            '沙河': {
                '老男孩': {},
                '北航': {},
            },
            '天通苑': {},
            '回龙观': {},
        },
        '朝阳': {},
        '东城': {},
    },
    '上海': {
        '闵行': {
            "人民广场": {
                '炸鸡店': {}
            }
        },
        '闸北': {
            '火车战': {
                '携程': {}
            }
        },
        '浦东': {},
    },
    '山东': {},
}
  • 递归函数实现三级菜单
def threeLM(dic):
    while True:
        for k in dic:print(k)
        key = input('input>>').strip()
        if key == 'b' or key == 'q':return key
        elif key in dic.keys() and dic[key]:
            ret = threeLM(dic[key])
            if ret == 'q': return 'q'


threeLM(menu)
  • 递归 堆栈实现
l = [menu]
while l:
    for key in l[-1]:print(key)
    k = input('input>>').strip()   # 北京
    if k in l[-1].keys() and l[-1][k]:l.append(l[-1][k])
    elif k == 'b':l.pop()
    elif k == 'q':break
posted @ 2018-09-26 14:30  小Q渺晓  阅读(978)  评论(0编辑  收藏  举报