py 5.18

一、递归函数

# import sys
# sys.setrecursionlimit(10000)
# count = 0
# def func(n):
#     n += 1
#     print(n)
#     func(n)
# func(count)
# def age(n):
#     if n == 1:
#         return 23
#     else:
#         return age(n-1) + 2
# print(age(4))
# 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]
# for i in range(len(l)):
#     if l[i] == 66:
#         print(i)
# l = [2,3,5,6,8,10]
View Code

二、二分查找

# def func(li,aim,start = 0,end =None):
#     end = len(li) if end == None else end #如果最开始查找,end = len()
#     min_index = (end - start) //2 + start
#     if start < end:
#         if aim > li[min_index]:
#             return func(li,aim,start = min_index+1,end = end)
#         elif aim < li[min_index]:
#             return func(li,aim,start = start,end=min_index)
#         elif aim == li[min_index]:
#             return min_index
#     else:
#         print(start,end)
#         return '没有此值'
# print(func(l,9))
View Code

猜你喜欢

转载自www.cnblogs.com/liujjpeipei/p/9057862.html
py