16 Python 递归例题

Posted 潘红伟

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了16 Python 递归例题相关的知识,希望对你有一定的参考价值。

递归

 

1.什么是递归 recursion 递归

-    

      递归的定义——在一个函数里再调用这个函数本身

 

      在一个函数里再调用这个函数本身,这种魔性的使用函数的方式就叫做递归

      递归的最大深度——997

    一个函数在内部调用自己

     递归的层数在python里是有限制的 997/998层

2.层数可以修改 sys模块

 

技术分享
 1 import sys  #python限制在997/998
 2 sys.setrecursionlimit(10000000)  #可以修改
 3 COUNT = 0
 4 def func():  #recursion 递归
 5     global COUNT
 6     COUNT += 1
 7     print(COUNT)
 8     func()
 9 
10 func()
View Code

3.解耦

要完成一个完整的功能,但这个功能的规模要尽量小,并且和这个功能无关的其他代码应该和这个函数分离
1.增强代码的重用性
2.减少代码变更的相互影响

4.实例一,求年龄
技术分享
 1 #写递归函数必须要有一个结束条件
 2 #alex
 3 #1 alex egon + 2   n=1  age(1) = age(2) + 2
 4 #2 egon wusir + 2  n=2  age(2) = age(3) +2
 5 #3 wusir 金鑫 + 2  n=3  age(3) = age(4) +2
 6 #4 金鑫 40         n=4  age(4) = 40
 7 
 8 def age(n):
 9     if n == 4:
10         return 40
11     return age(n+1)+2
12 
13 # age(1)   #46
14 # def age(1):
15 #     return 46
16 # 
17 # def age(2):
18 #     return 44
19 # 
20 # def age(3):
21 #     return 42
22 # 
23 # def age(4):
24 #     if 4 == 4:
25 #         return 40
26 
27 复制代码
View Code

5.实例二,求阶乘

 

技术分享
 1 #求阶乘 n = 7  7*6*5*4*3*2*1
 2 def func(n):
 3     if n == 1:
 4         return 1
 5     else:
 6         return n*func(n-1)
 7 
 8 ret = func(4)
 9 print(ret)
10 
11 # #n = 4
12 # def func(4):
13 #     return 4*6
14 #
15 # #n = 3
16 # def func(3):
17 #     return 6
18 #
19 # #n = 2
20 # def func(2):
21 #         return 2
22 #
23 # #n = 1
24 # def func(n):
25 #     if n == 1:
26 #         return 1
View Code

 

6.实例三,二分查找

 

技术分享
 1 #算法  计算的方法
 2 def search(num,l,start=None,end=None):
 3     start = start if start else 0
 4     end = end if end else len(l) - 1
 5     mid = (end - start)//2 + start    #这里要加上开始的索引
 6     if start > end:   #如果差找不到返回None
 7         return None
 8     elif l[mid] > num :   #17,17
 9         return search(num,l,start,mid-1)
10     elif l[mid] < num:
11         return search(num,l,mid+1,end)
12     elif l[mid] == num:
13         return mid
14 
15 
16 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]
17 print(search(66,l))
18 
19 def search(num,l,start=None,end=None): #66,[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]
20     start = start if start else 0      #start = 0
21     end = end if end else len(l) - 1   #end = 24
22     mid = (end - start)//2 + start     #mid = 12
23     if l[mid] > num :                  #l[mid] = 41  <  66
24         search(num,l,start,mid-1)
25     elif l[mid] < num:
26         ret = search(num,l,mid+1,end)        #search(66,l,13,24)
27         return ret
28     elif l[mid] == num:
29         return mid, l[mid]
30 
31 def search(num,l,start=None,end=None): #66,[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]
32     start = start if start else 0      #start = 13
33     end = end if end else len(l) - 1   #end = 24
34     mid = (end - start)//2 + start     #mid = 18
35     if l[mid] > num :                  #l[mid] = 67  >  66
36         search(num,l,start,mid-1)      #search(66,l,13,17)
37     elif l[mid] < num:
38         ret = search(num,l,mid+1,end)
39         return ret
40     elif l[mid] == num:
41         return mid, l[mid]
42 
43 def search(num,l,start=None,end=None): #66,[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]
44     start = start if start else 0      #start = 13
45     end = end if end else len(l) - 1   #end = 17
46     mid = (end - start)//2 + start     #mid = 15
47     if l[mid] > num :                  #l[mid] = 56  <  66
48         search(num,l,start,mid-1)
49     elif l[mid] < num:
50         ret = search(num,l,mid+1,end)        #search(66,l,16,17)
51         return ret
52     elif l[mid] == num:
53         return mid, l[mid]
54 
55 def search(num,l,start=None,end=None): #66,[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]
56     start = start if start else 0      #start = 16
57     end = end if end else len(l) - 1   #end = 17
58     mid = (end - start)//2 + start     #mid = 16
59     if l[mid] > num :                  #l[mid] = 56  <  66
60         search(num,l,start,mid-1)
61     elif l[mid] < num:
62         ret = search(num,l,mid+1,end)        #search(66,l,17,17)
63         return ret
64     elif l[mid] == num:
65         return mid, l[mid]
66 
67 def search(num,l,start=None,end=None): #66,[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]
68     start = start if start else 0      #start = 17
69     end = end if end else len(l) - 1   #end = 17
70     mid = (end - start)//2 + start     #mid = 17
71     if l[mid] > num :                  #l[mid] = 66  ==  66
72         search(num,l,start,mid-1)
73     elif l[mid] < num:
74         search(num,l,mid+1,end)
75     elif l[mid] == num:
76         return mid, l[mid]               #return 17,66
View Code

 

7实例四,斐波那契数列

 

技术分享
 1 #斐波那契
 2 #1,1,2,3,5,8
 3 def fib(n):
 4     if n==1 or n==2:
 5         return 1
 6     return fib(n-1) + fib(n-2)
 7 
 8 # print(fib(6))
 9 
10 # def fib(6):
11 #     return 5 + 3
12 #
13 # def fib(5):
14 #     return 5
15 #
16 # def fib(4):
17 #     return 3
18 #
19 # def fib(3):
20 #     return 2
21 #
22 # def fib(2):
23 #     if n==1 or n==2:
24 #         return 1
25 #
26 # def fib(1):
27 #     if n == 1 or n == 2:
28 #         return 1
View Code

 

8.实例五,面试真题

 

技术分享
 1 #面试真题
 2 
 3 # 有?个数据结构如下所示,请编写?个函数从该结构数据中返回由指定的字段和对应的值组成的字典。如果指定字段不存在,则跳过该字段。(10分)
 4 data={"time":"2016-08-05T13:13:05",
 5     "some_id":"ID1234",
 6     "grp1":{ "fld1":1,"fld2":2},
 7     "xxx2":{ "fld3":0,"fld5":0.4},
 8     "fld6":11,
 9     "fld7":7,
10     "fld46":8}
11 #fields:由"|"连接的以"fld"开头的字符串,如:fld2|fld3|fld7|fld19
12 
13 def select(data,fields,result = {}):  #这里默认参数创建一个空字典,利用了默认参数可变类型的陷阱,修改字典始终修改的是一个字典
14     field_lst = fields.split(|)
15     for key in data:
16         if key in field_lst:
17             result[key] = data[key]
18         elif type(data[key]) == dict:
19             select(data[key], fields)
20     return result
21 
22 fields = fld2|fld3|fld7|fld19
23 ret = select(data,fields)
24 print(ret)
25 
26 
27 #方法二
28 def select(data,fields):
29     result = {}
30     field_lst = fields.split(|)
31     for key in data:
32         if key in field_lst:
33             result[key] = data[key]
34         elif type(data[key]) == dict:
35             res = select(data[key],fields)
36             result.update(res)
37     return result
View Code

 

9.实例六,三级菜单

 

技术分享
 1 #3.三级菜单
 2 menu = {
 3     北京: {
 4         海淀: {
 5             五道口: {
 6                 soho: {},
 7                 网易: {},
 8                 google: {}
 9             },
10             中关村: {
11                 爱奇艺: {},
12                 汽车之家: {},
13                 youku: {},
14             },
15             上地: {
16                 百度: {},
17             },
18         },
19         昌平: {
20             沙河: {
21                 老男孩: {},
22                 北航: {},
23             },
24             天通苑: {},
25             回龙观: {},
26         },
27         朝阳: {},
28         东城: {},
29     },
30     上海: {
31         闵行: {
32             "人民广场": {
33                 炸鸡店: {}
34             }
35         },
36         闸北: {
37             火车战: {
38                 携程: {}
39             }
40         },
41         浦东: {},
42     },
43     山东: {},
44 }
45 # 相同的数据类型 嵌套在一起
46 def Three_Level_Menu(menu):
47     while True:
48         for k in menu:print(k)
49         key = input(>>>)
50         if key == q:return q   #到最上层接收的q,遇到return结束,返回q有没有人接收都没关系
51         elif key == b:break  #如果输入b,跳出本层循环
52         elif key in menu:
53             ret = Three_Level_Menu(menu[key])
54             if ret == q: return q    #如果接收到的是q,也往上返回一个q
55 Three_Level_Menu(menu)
View Code

 






以上是关于16 Python 递归例题的主要内容,如果未能解决你的问题,请参考以下文章

Python基础——递归及其经典例题(阶乘斐波那契数列汉诺塔)

python递归函数

python-递归函数

递归调用 LeetCodel例题分析

python学习day16

python - - 函数 - - 递归函数