python ------- recursive function

A recursive definition

1. What is recursive: in a function call to the function itself

2. Maximum number of layers to do a recursive limit: 997, but you can own limitations

Verify 997

  1 def  foo():
  2     print(n)
  3     n+=1
  4     foo(n)
  5 foo(1)

3. The maximum number of layers limit python is the default, you can make changes, but do not recommend that you modify. (Because if the problem does not solve the recursive layer 997 are either not suitable for use recursion to solve the problem, or is that your code sucks)

  . 1  Import  SYS 
  2  SYS . Setrecursionlimit (10000000) modified # recursive layers
   . 3 n-= 0
   . 4  DEF F ():
   . 5      Global n-
   . 6      n-+ =. 1
   . 7      Print (n-)
   . 8      F ()
   . 9 F ()
View Code

We can code above embodiment sys module introduced to modify the maximum depth of recursion.

sys module: and all settings and methods associated python

4. End recursive logo: return

The problem is solved by recursive parameters to control the size of each call to reduce computing

6. usage scenarios: reduce the size of data, but the way to solve the problem has not changed

7. Many sorting algorithms will be used recursively

Second, the application of recursive small

1. Let's guess Xiao Ming's age

    Xiao Ming is the new student, Lily asked him how old.

    He said: I do not tell you, but I'm two years older than surging.

    Surging said: Neither will I tell you, I'm two years older than Xiaoxiao

    Xiaoxiao said: Neither will I tell you, I'm two years older than the little star

    Little Star did not tell him to say: I'm two years older than Xiaohua

    Finally Xiaohua said, I tell you, I am 18 years old

This how to do it? Of course, some would say, this is very simple ah, know Xiaohua, will know little star, a small star will know know Xiaoxiao, and so, you know that Bob's age friends. This process has been very close to the idea of ​​recursion.

Xiaohua 18+2
small star 20+2
Xiaoxiao  22+2
surging 24+2
Hsiao Ming 26+2

The above figure we can use a number to represent it

  1 age(5) = age(4)+2
  2 age(4) = age(3) + 2
  3 age(3) = age(2) + 2
  4 age(2) = age(1) + 2
  5 age(1) = 18

Then the code how to write it?

  1 def age(n):
  2     if n == 1:
  3         return 18
  4     else:
  5         return age(n - 1) + 2
  6 
  7 ret=age(6)
  8 print(ret)
View Code

2.一个数,除2直到不能整除2

  1 def  cal(num):
  2         if  num%2==0:#先判断能不能整除
  3             num=num//2
  4             return cal(num)
  5         else:
  6             return num
  7 print(cal(8))
  8 
  9 
一个数,除2直到不能整除2 View Code

3.如果一个数可以整除2,就整除,不能整除就*3+1

  1 def func(num):
  2     print(num)
  3     if num==1:
  4         return
  5     if num%2==0:
  6         num=num//2
  7     else:
  8         num=num*3+1
  9     func(num)
 10 func(5)
 11 
 12 
如果一个数可以整除2,就整除,不能整除就*3+1 View Code

 

三、三级菜单

menu = {
'北京': {
'海淀': {
'五道口': {
'soho': {},
'网易': {},
'google': {}
},
'中关村': {
'爱奇艺': {},
'汽车之家': {},
'youku': {},
},
'上地': {
'百度': {},
},
},
'昌平': {
'沙河': {
'老男孩': {},
'北航': {},
},
'天通苑': {},
'回龙观': {},
},
'朝阳': {},
'东城': {},
},
'上海': {
'闵行': {
"人民广场": {
'炸鸡店': {}
}
},
'闸北': {
'火车战': {
'携程': {}
}
},
'浦东': {},
},
'山东': {},
}

 

  1 def threeLM(menu):
  2     while True:
  3         for key in menu:#循环字典的key,打印出北京,上海,山东
  4             print(key)
  5         name=input('>>>:').strip()
  6         if name=='back' or name=='quit':#如果输入back,就返回上一层。如果输入quit就退出
  7             return name #返回的name的给了ret
  8         if name in menu:
  9             ret=threeLM(menu[name])
 10             if ret=='quit':return 'quit'#如果返回的是quit,就直接return quit 了,就退出了
 11 threeLM()
 12 # print(threeLM(menu))#print打印了就返回出quit了,threeLM()没有打印就直接退出了
 13 
 14 
三级菜单 View Code

四、二分查找算法

从这个列表中找到55的位置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】

这就是二分查找,从上面的列表中可以观察到,这个列表是从小到大依次递增的有序列表。

按照上面的图就可以实现查找了。

  1 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]
  2 def find(l,aim):
  3     mid=len(l)//2#取中间值,//长度取整(取出来的是索引)
  4     if l[mid]>aim:#判断中间值和要找的那个值的大小关系
  5         new_l=l[:mid]#顾头不顾尾
  6         return find(new_l,aim)#递归算法中在每次函数调用的时候在前面加return
  7     elif l[mid]<aim:
  8         new_l=l[mid+1:]
  9         return find(new_l,aim)
 10     else:
 11         return l[mid]
 12 print(find(l,66))
 13 
 14 
简单的二分法 View Code

 

  1 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]
  2 def func(l, aim,start = 0,end = len(l)-1):
  3     mid = (start+end)//2#求中间的数
  4     if not l[start:end+1]:#如果你要找的数不在里面,就return'你查找的数字不在这个列表里面'
  5         return  '你查找的数字不在这个列表里面'
  6     elif aim > l[mid]:
  7         return func(l,aim,mid+1,end)
  8     elif aim < l[mid]:
  9         return func(l,aim,start,mid-1)
 10     elif aim == l[mid]:
 11         print("bingo")
 12         return mid
 13 
 14 index = func(l,55)
 15 print(index)
 16 # print(func(l,41))
 17 
 18 升级版二分法
升级版二分法 View Code

 

 

归类: python相关

Guess you like

Origin www.cnblogs.com/lz1996/p/11572340.html