二分查找
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, start = 0, end = None):
3 end = len(l) if end is None else end
4 mid_num = (end - start) // 2 + start
5 if start > end:
6 if aim > l[mid_num]:
7 return find(l, aim, start = mid_num + 1, end = end)
8 elif aim < l[mid_num]:
9 return find(l, aim, start = start, end = mid_num - 1)
10 else:
11 return mid_num
12 else:
13 return '无此数'
14 print(l[12])
15 print(find(l,44))
16 # 41
17 # 无此数
三级菜单
1 menu = {
2 '北京': {
3 '海淀': {
4 '五道口': {
5 'soho': {},
6 '网易': {},
7 'google': {}
8 },
9 '中关村': {
10 '爱奇艺': {},
11 '汽车之家': {},
12 'youku': {},
13 },
14 '上地': {
15 '百度': {},
16 },
17 },
18 '昌平': {
19 '沙河': {
20 '老男孩': {},
21 '北航': {},
22 },
23 '天通苑': {},
24 '回龙观': {},
25 },
26 '朝阳': {},
27 '东城': {},
28 },
29 '上海': {
30 '闵行': {
31 "人民广场": {
32 '炸鸡店': {}
33 }
34 },
35 '闸北': {
36 '火车战': {
37 '携程': {}
38 }
39 },
40 '浦东': {},
41 },
42 '山东': {},
43 }
44 def threeLM(dic):
45 while True:
46 for k in dic:print(k)
47 key = input('input>>').strip()
48 if key == 'b' or key == 'q':return key
49 elif key in dic.keys() and dic[key]:
50 ret = threeLM(dic[key])
51 if ret == 'q': return 'q'
56 threeLM(menu)
斐波那契数列
1 def fib(n):
2 if n == 1 or n ==2:
3 return 1
4 else:
5 return fib(n-1) + fib(n - 2)
6 print(fib(6))
7 # 8