day15
递归函数:在一个函数里在调用这个函数本身。
递归的最大深度:998
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
def age(n): if n == 1: return 40 else: return age(n-1)+2 print(age(4))
二分查找
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: return None elif l[mid] > num : return search(num,l,start,mid-1) elif l[mid] < num: return search(num,l,mid+1,end) elif l[mid] == num: return mid
课上讲解
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
def two_search(li, aim, start=0, end=None): end = len(li)-1 if end is None else end mid_index = (end - start) // 2 + start # 3 if start <= end: if li[mid_index] < aim: return two_search(li,aim,start=mid_index+1,end=end) elif li[mid_index] > aim: return two_search(li,aim,start=0,end=mid_index-1) #([2,3,5],3) elif li[mid_index] == aim: return mid_index else: return '没有此值' else: return '没有此值'