标签:else 好的 nbsp lex pytho tar int 简单 函数
递归函数
了解什么是递归 : 在函数中调用自身函数
最大递归深度默认是997/998 —— 是python从内存角度出发做得限制
递归的缺点 : 占内存
递归的优点: 会让代码变简单
# alex 多大 n = 1 age(1) = age(2)+2 = age(n+1) + 2 # alex比egon大两岁 # egon多大? n = 2 age(2) = age(3) + 2 = age(n+1) +2 # egon比wusir大两岁 # wusir多大 n = 3 age(3) = age(4) + 2 = age(n+1) +2 # wusir比金老板大两岁 # 金老板多大? # 金老板40了 n = 4 age(4) = 40 # n = 4 age(4) = 40 # n <4 age(n) = age(n+1) +2 def age(n): if n == 4: return 40 elif n >0 and n < 4: age(n+1) + 2 # print(age(1)) # # 教你看递归 # def age(1): # if 1 == 4: # return 40 # elif 1 > 0 and 1 < 4: # return 46 # # def age(2): # if 2 == 4: # return 40 # elif 2 >0 and 2 < 4: # age(3) + 2 None +2 # # def age(3): # if 3 == 4: # return 40 # elif 3 >0 and 3 < 4: # 42 # # def age(4): # if 4 == 4: # return 40 # elif n >0 and n < 4: # age(n+1) + 2
了解基础的算法 才能创造出更好的算法
不是所有的事情都能套用现成的方法解决的
有些时候会用到学过的算法知识来解决新的问题
# 二分查找算法 必须处理有序的列表 # 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] # 5000000 4999998 # 代码实现 # def find(l,aim): # mid_index = len(l) // 2 # if l[mid_index] < aim: # new_l = l[mid_index+1 :] # find(new_l,aim) # elif l[mid_index] > aim: # new_l = l[:mid_index] # find(new_l, aim) # else: # print(‘找到了‘,mid_index,l[mid_index]) # # find(l,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] # def find(l,aim,start = 0,end = None): # end = len(l) if end is None else end # end = len(l) 24 # mid_index = (end - start)//2 + start #计算中间值 12 + 0 = 12 # if l[mid_index] < aim: #l[12] < 44 #41 < 44 # find(l,aim,start =mid_index+1,end=end) # find(l,44,start=13,end=24) # elif l[mid_index] > aim: # find(l, aim, start=start, end=mid_index-1) # else: # print(‘找到了‘,mid_index,aim) # # def find(l,aim,start = 0,end = None): # l,44,start=13,end=24 # end = len(l) if end is None else end # end = 24 # mid_index = (end - start)//2 + start #计算中间值 24-13/2 = 5 + 13 = 18 # if l[mid_index] < aim: #l[18] < 44 #67 < 44 # find(l,aim,start =mid_index+1,end=end) # elif l[mid_index] > aim: # 67 > 44 # find(l, aim, start=start, end=mid_index-1) # find(l,44,start=13,end=17) # else: # print(‘找到了‘,mid_index,aim) # # def find(l,aim,start = 0,end = None): # l,44,start=13,end=17 # end = len(l) if end is None else end # end = 17 # mid_index = (end - start)//2 + start #计算中间值 17-13/2 = 2 + 13 = 15 # if l[mid_index] < aim: #l[15] < 44 #55 < 44 # find(l,aim,start =mid_index+1,end=end) # elif l[mid_index] > aim: # 55 > 44 # find(l, aim, start=start, end=mid_index-1) # find(l,44,start=13,end=14) # else: # print(‘找到了‘,mid_index,aim) # # def find(l,aim,start = 0,end = None): # l,44,start=13,end=14 # end = len(l) if end is None else end # end = 14 # mid_index = (end - start)//2 + start #计算中间值 14-13/2 = 0+ 13 = 13 # if l[mid_index] < aim: #l[13] < 44 #42 < 44 # find(l,aim,start =mid_index+1,end=end) # find(l,44,start=14,end=14) # elif l[mid_index] > aim: # 42 > 44 # find(l, aim, start=start, end=mid_index-1) # else: # print(‘找到了‘,mid_index,aim) 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 =mid_index+1,end=end) elif l[mid_index] > aim: return find(l, aim, start=start, end=mid_index-1) else: return mid_index else: return ‘找不到这个值‘ ret= find(l,44) print(ret)
标签:else 好的 nbsp lex pytho tar int 简单 函数
原文地址:https://www.cnblogs.com/qi1113/p/12354506.html