1. 程式人生 > >py 5.18

py 5.18

set += AS 開始 imp index dex 5.1 spa

一、遞歸函數

技術分享圖片
# 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

py 5.18