1.递归函数:
1.简单的递归函数
def func(): print(666) return func() func()
2.递归的最大深度
执行一次,创建一个临时空间,且不关闭,无线的执行调用.
RecursionError: maximum recursion depth exceeded while calling a Python object
递归:执行次数是998/997 解释器会主动强制停止,报错.保护内存.
![](https://i-blog.csdnimg.cn/blog_migrate/8f900a89c6347c561fdf2122f13be562.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/961ddebeb323a10fe0623af514929fc1.gif)
count=0 def func(): global count count+=1 print(count) func() func()
3.更改最大深度
import sys
sys.setrecursionlimit(100000) 一般不提倡修改
4.递归函数:
优点:复杂的逻辑用递归非常简单.
缺点:递归次数多了,占用内存,费时间.
实例:
![](https://i-blog.csdnimg.cn/blog_migrate/8f900a89c6347c561fdf2122f13be562.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/961ddebeb323a10fe0623af514929fc1.gif)
1.求age def age(n): if n==4: return 18 elif n>0 and n<4: return age(n+1)+2 print(age(1) ) 2.#求阶乘:用递归函数和普通函数 def func(x): if x==1:return 1 else:return func(x-1)*x print(func(5)) def cal(x): sum=1 for i in range(1,x+1): sum=sum*i return sum print(cal(5))
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]
在有序的列表中查找到66所在的位置
方法一: 循环 # 5000000 4999999
方法二: l.index(66) #最正确的方法
![](https://i-blog.csdnimg.cn/blog_migrate/8f900a89c6347c561fdf2122f13be562.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/961ddebeb323a10fe0623af514929fc1.gif)
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 func(l,aim,start,end): mid=(end - start) // 2 + start if l[mid] > aim: func(l,aim,start=start,end=mid) elif l[mid] < aim: func(l,aim,start=mid+1,end=end) elif l[mid] == aim: print(mid,aim,l[mid]) func(l,66,0,len(l)-1)
以上程序存在问题 1.查的值不存在
![](https://i-blog.csdnimg.cn/blog_migrate/8f900a89c6347c561fdf2122f13be562.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/961ddebeb323a10fe0623af514929fc1.gif)
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 func(l,aim,start,end): if start <= end: mid=(end - start) // 2 + start if l[mid] > aim: func(l,aim,start=start,end=mid) elif l[mid] < aim: func(l,aim,start=mid+1,end=end) elif l[mid] == aim: print(mid,aim,l[mid]) else:print("你要找的值不存在") func(l,66,0,len(l)-1)
2.参数的简化
![](https://i-blog.csdnimg.cn/blog_migrate/8f900a89c6347c561fdf2122f13be562.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/961ddebeb323a10fe0623af514929fc1.gif)
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 func(l,aim,start=0,end=None): if end==None:end=len(l)-1 if start <= end: mid=(end - start) // 2 + start if l[mid] > aim: func(l,aim,start=start,end=mid) elif l[mid] < aim: func(l,aim,start=mid+1,end=end) elif l[mid] == aim: print(mid,aim,l[mid]) else:print("你要找的值不存在") func(l,66)
3.返回值的问题
![](https://i-blog.csdnimg.cn/blog_migrate/8f900a89c6347c561fdf2122f13be562.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/961ddebeb323a10fe0623af514929fc1.gif)
def find(l,aim,start=0,end=None): if end == None:end = len(l)-1 if start <= end: mid = (end - start) // 2 + start if l[mid] > aim: return find(l,aim,start=start,end=mid-1) elif l[mid] < aim: return find(l,aim,start=mid+1,end=end) elif l[mid] == aim: return mid #返回给了上一级 else: return None 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('ret :',find(l,18)) #ret : 6 #不加return def find(l,aim,start=0,end=None): if end == None:end = len(l)-1 if start <= end: mid = (end - start) // 2 + start if l[mid] > aim: find(l,aim,start=start,end=mid-1) elif l[mid] < aim: find(l,aim,start=mid+1,end=end) elif l[mid] == aim: return mid #返回给了上一级 else: return None 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('ret :',find(l,18)) #ret : None