递归
递归
1.什么是递归 recursion 递归
- 一个函数在内部调用自己
- 递归的层数在python里是有限制的 997/998层
2.层数可以修改 sys模块
import sys #python限制在997/998
sys.setrecursionlimit(10000000) #可以修改
COUNT = 0
def func(): #recursion 递归
global COUNT
COUNT += 1
print(COUNT)
func()
func()
3.解耦
要完成一个完整的功能,但这个功能的规模要尽量小,并且和这个功能无关的其他代码应该和这个函数分离
1.增强代码的重用性
2.减少代码变更的相互影响
4.实例一,求年龄
#写递归函数必须要有一个结束条件 #alex #1 alex egon + 2 n=1 age(1) = age(2) + 2 #2 egon wusir + 2 n=2 age(2) = age(3) +2 #3 wusir 金鑫 + 2 n=3 age(3) = age(4) +2 #4 金鑫 40 n=4 age(4) = 40 def age(n): if n == 4: return 40 return age(n+1)+2 # age(1) #46 # def age(1): # return 46 # # def age(2): # return 44 # # def age(3): # return 42 # # def age(4): # if 4 == 4: # return 40
5.实例二,求阶乘
#求阶乘 n = 7 7*6*5*4*3*2*1 def func(n): if n == 1: return 1 else: return n*func(n-1) ret = func(4) print(ret) # #n = 4 # def func(4): # return 4*6 # # #n = 3 # def func(3): # return 6 # # #n = 2 # def func(2): # return 2 # # #n = 1 # def func(n): # if n == 1: # return 1
6.实例三,二分查找
#算法 计算的方法 def search(num,l,start=None,end=None): start = start if start else 0 end = end if end else len(l) - 1 mid = (end - start)//2 + start #这里要加上开始的索引 if start > end: #如果差找不到返回None return None elif l[mid] > num : #17,17 return search(num,l,start,mid-1) elif l[mid] < num: return search(num,l,mid+1,end) elif l[mid] == num: return mid 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(search(66,l)) 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] start = start if start else 0 #start = 0 end = end if end else len(l) - 1 #end = 24 mid = (end - start)//2 + start #mid = 12 if l[mid] > num : #l[mid] = 41 < 66 search(num,l,start,mid-1) elif l[mid] < num: ret = search(num,l,mid+1,end) #search(66,l,13,24) return ret elif l[mid] == num: return mid, l[mid] 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] start = start if start else 0 #start = 13 end = end if end else len(l) - 1 #end = 24 mid = (end - start)//2 + start #mid = 18 if l[mid] > num : #l[mid] = 67 > 66 search(num,l,start,mid-1) #search(66,l,13,17) elif l[mid] < num: ret = search(num,l,mid+1,end) return ret elif l[mid] == num: return mid, l[mid] 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] start = start if start else 0 #start = 13 end = end if end else len(l) - 1 #end = 17 mid = (end - start)//2 + start #mid = 15 if l[mid] > num : #l[mid] = 56 < 66 search(num,l,start,mid-1) elif l[mid] < num: ret = search(num,l,mid+1,end) #search(66,l,16,17) return ret elif l[mid] == num: return mid, l[mid] 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] start = start if start else 0 #start = 16 end = end if end else len(l) - 1 #end = 17 mid = (end - start)//2 + start #mid = 16 if l[mid] > num : #l[mid] = 56 < 66 search(num,l,start,mid-1) elif l[mid] < num: ret = search(num,l,mid+1,end) #search(66,l,17,17) return ret elif l[mid] == num: return mid, l[mid] 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] start = start if start else 0 #start = 17 end = end if end else len(l) - 1 #end = 17 mid = (end - start)//2 + start #mid = 17 if l[mid] > num : #l[mid] = 66 == 66 search(num,l,start,mid-1) elif l[mid] < num: search(num,l,mid+1,end) elif l[mid] == num: return mid, l[mid] #return 17,66
def bin_search_rec(data_set, value, low=None, high=None): low = low if low else 0 high = high if high else len(data_set)-1 if low <= high: mid = (low + high) // 2 if data_set[mid]==value: return mid elif data_set[mid]>value: return bin_search_rec(data_set,value,low,mid-1) else: return bin_search_rec(data_set,value,mid+1,high) else: return None
7实例四,斐波那契数列
#斐波那契 #1,1,2,3,5,8 def fib(n): if n==1 or n==2: return 1 return fib(n-1) + fib(n-2) # print(fib(6)) # def fib(6): # return 5 + 3 # # def fib(5): # return 5 # # def fib(4): # return 3 # # def fib(3): # return 2 # # def fib(2): # if n==1 or n==2: # return 1 # # def fib(1): # if n == 1 or n == 2: # return 1
8.实例五,面试真题
#面试真题 # 有⼀个数据结构如下所示,请编写⼀个函数从该结构数据中返回由指定的字段和对应的值组成的字典。如果指定字段不存在,则跳过该字段。(10分) data={"time":"2016-08-05T13:13:05", "some_id":"ID1234", "grp1":{ "fld1":1,"fld2":2}, "xxx2":{ "fld3":0,"fld5":0.4}, "fld6":11, "fld7":7, "fld46":8} #fields:由"|"连接的以"fld"开头的字符串,如:fld2|fld3|fld7|fld19 def select(data,fields,result = {}): #这里默认参数创建一个空字典,利用了默认参数可变类型的陷阱,修改字典始终修改的是一个字典 field_lst = fields.split('|') for key in data: if key in field_lst: result[key] = data[key] elif type(data[key]) == dict: select(data[key], fields) return result fields = 'fld2|fld3|fld7|fld19' ret = select(data,fields) print(ret) #方法二 def select(data,fields): result = {} field_lst = fields.split('|') for key in data: if key in field_lst: result[key] = data[key] elif type(data[key]) == dict: res = select(data[key],fields) result.update(res) return result
9.实例六,二级菜单
#3.三级菜单 menu = { '北京': { '海淀': { '五道口': { 'soho': {}, '网易': {}, 'google': {} }, '中关村': { '爱奇艺': {}, '汽车之家': {}, 'youku': {}, }, '上地': { '百度': {}, }, }, '昌平': { '沙河': { '老男孩': {}, '北航': {}, }, '天通苑': {}, '回龙观': {}, }, '朝阳': {}, '东城': {}, }, '上海': { '闵行': { "人民广场": { '炸鸡店': {} } }, '闸北': { '火车战': { '携程': {} } }, '浦东': {}, }, '山东': {}, } # 相同的数据类型 嵌套在一起 def Three_Level_Menu(menu): while True: for k in menu:print(k) key = input('>>>') if key == 'q':return 'q' #到最上层接收的q,遇到return结束,返回q有没有人接收都没关系 elif key == 'b':break #如果输入b,跳出本层循环 elif key in menu: ret = Three_Level_Menu(menu[key]) if ret == 'q': return 'q' #如果接收到的是q,也往上返回一个q Three_Level_Menu(menu)
既然选择了远方,便是风雨兼程...