1 def func(arg1,arg2,stop): 2 if arg1 == 0 : 3 print(arg1) 4 print(arg2) 5 6 arg3 = arg1 + arg2 7 8 if arg3 < stop: 9 print(arg3) 10 func(arg2,arg3,stop) 11 12 func(0,1,50) 13 >>> 14 0 15 1 16 1 17 2 18 3 19 5 20 8 21 13 22 21 23 34
二分查找:數組
1 def binary_serach(data_source,find_num): 2 3 mid = int(len(data_source)/2) 4 5 if len(data_source) >= 1: 6 if data_source[mid] > find_num: 7 print("data in left of %s" % data_source[:mid]) 8 binary_serach(data_source[:mid],find_num) 9 elif data_source[mid] < find_num: 10 print("data in right of %s" % data_source[mid:]) 11 binary_serach(data_source[mid:],find_num) 12 else: 13 print("found the number:%s" % find_num) 14 else: 15 print("Can't find ...") 16 17 if __name__ == '__main__': 18 data = list(range(3,30,3)) 19 find_num = 3 20 # if find_num == data[0]: 21 # print("found the number:%s" % find_num) 22 # else: 23 binary_serach(data,find_num)
二維數組的90度旋轉跳過:spa